Skip to content

Commit

Permalink
Text.Lexer: add newline and comment lexers
Browse files Browse the repository at this point in the history
Adds `newline`, `newlines`. They recognise "\r\n", "\r", or "\n" as a newline.

Adds `blockComment` and `lineComment`.
  • Loading branch information
msmorgan committed Sep 6, 2017
1 parent 7e604f8 commit 7c2148d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions libs/contrib/Text/Lexer.idr
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ export
spaces : Lexer
spaces = some space

||| Recognise a single newline sequence. Understands CRLF, CR, and LF
export
newline : Lexer
newline = let crlf = "\r\n" in
exact crlf <|> oneOf crlf

||| Recognise one or more newline sequences. Understands CRLF, CR, and LF
export
newlines : Lexer
newlines = some newline

||| Recognise a single non-whitespace, non-alphanumeric character
export
symbol : Lexer
Expand Down Expand Up @@ -341,6 +352,25 @@ export
hexLit : Lexer
hexLit = approx "0x" <+> hexDigits

||| Recognise `start`, then recognise all input until a newline is encountered,
||| and consume the newline. Will succeed if end-of-input is encountered before
||| a newline.
export
lineComment : (start : Lexer) -> Lexer
lineComment start = start <+> manyUntil newline any <+> opt newline

||| Recognise all input between `start` and `end` lexers.
||| Supports balanced nesting.
|||
||| For block comments that don't support nesting (such as C-style comments),
||| use `surround`.
export
blockComment : (start : Lexer) -> (end : Lexer) -> Lexer
blockComment start end = start <+> middle <+> end
where
middle : Recognise False
middle = manyUntil end (blockComment start end <|> any)

||| A mapping from lexers to the tokens they produce.
||| This is a list of pairs `(Lexer, String -> tokenType)`
||| For each Lexer in the list, if a substring in the input matches, run
Expand Down

0 comments on commit 7c2148d

Please sign in to comment.