Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more parsers and a split function. #1330

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/parser.dx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def parse_any() ->> Parser Char = MkParser \h.
h.offset := i + 1
c'

def parse_anything_but(c:Char) -> Parser Char =
MkParser \h.
i = get h.offset
c' = index_list h.input i
assert (c /= c')
h.offset := i + 1
c'

def try(parser:Parser a) -> Parser a given (a) = MkParser \h.
savedPos = get h.offset
ans = catch \. parse h parser
Expand Down Expand Up @@ -101,6 +109,12 @@ def parse_many(parser:Parser a) -> Parser (List a) given (a|Data) = MkParser \h.
push results x
Continue

def parse_some(parser:Parser a) -> Parser (List a) given (a|Data) =
MkParser \h.
c = parse h parser
rest = parse h $ parse_many parser
AsList(_, [c]) <> rest

def parse_unsigned_int() ->> Parser Int = MkParser \h.
AsList(_, digits) = parse h $ parse_many parse_digit
yield_state 0 \ref.
Expand All @@ -122,3 +136,16 @@ def bracketed(l:Parser (), r:Parser (), body:Parser a) -> Parser a given (a) =

def parens(parser:Parser a) -> Parser a given (a) =
bracketed (p_char '(') (p_char ')') parser

def split(space:Char, s:String) -> List String =
def trailing_spaces(space:Parser (), body:Parser a) -> Parser a given (a) =
MkParser \h.
ans = parse h body
_ = parse h $ parse_many space
ans
split_parser = MkParser \h.
_ = parse h $ parse_many (p_char space)
parse h $ parse_many (trailing_spaces (p_char space) (parse_some (parse_anything_but space)))
case run_parser s split_parser of
Just l -> l
Nothing -> AsList _ []
3 changes: 3 additions & 0 deletions tests/parser-combinator-tests.dx
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ def parserTFTriple() ->> Parser (Fin 3=>Bool) = MkParser \h.

:p run_parser "-1389" $ parse_int
> (Just -1389)

split ' ' " This is a sentence. "
> (AsList 4 ["This", "is", "a", "sentence."])
Loading