on master help sign in
Merge pull request #4027 from msmorgan/feature/lexer-opt
b0dddf0Jan de Muijnck-Hughes
libs/contrib/Text/Lexer.idr+12−9
@@ -48,18 +48,24 @@ export
48isNot : Char -> Lexer
48isNot : Char -> Lexer
49isNot x = Pred (/=x)
49isNot x = Pred (/=x)
50
50
51+||| Recognise a lexer or recognise no input. This is not guaranteed
52+||| to consume input
53+export
54+opt : Lexer -> Recognise False
55+opt l = l <|> Empty
56+
51||| Recognise a sequence of at least one sub-lexers
57||| Recognise a sequence of at least one sub-lexers
52export
58export
53some : Lexer -> Lexer
59some : Lexer -> Lexer
54−some l = l <+> (some l <|> Empty)
60+some l = l <+> opt (some l)
55
61
56||| Recognise a sequence of at zero or more sub-lexers. This is not
62||| Recognise a sequence of at zero or more sub-lexers. This is not
57||| guaranteed to consume input
63||| guaranteed to consume input
58export
64export
59many : Lexer -> Recognise False
65many : Lexer -> Recognise False
60−many l = some l <|> Empty
66+many l = opt (some l)
61
67
62−||| Recognize the first matching lexer from a non-empty list.
68+||| Recognise the first matching lexer from a non-empty list.
63export
69export
64choice : (xs : List Lexer) -> {auto ok : NonEmpty xs} -> Lexer
70choice : (xs : List Lexer) -> {auto ok : NonEmpty xs} -> Lexer
65choice (x :: []) = x
71choice (x :: []) = x
@@ -70,11 +76,8 @@ choice (x :: xs@(_ :: _)) = x <|> choice xs
70|||
76|||
71||| Useful for defining comments.
77||| Useful for defining comments.
72export
78export
73−manyTill : (l : Lexer)
74− -> (end : Lexer) -> Recognise False
79+manyTill : (l : Lexer) -> (end : Lexer) -> Recognise False
75−manyTill l end = end
80+manyTill l end = end <|> opt (l <+> manyTill l end)
76− <|> (l <+> manyTill l end)
77− <|> Empty
78
81
79||| Recognise any character
82||| Recognise any character
80export
83export
@@ -182,7 +185,7 @@ charLit = is '\'' <+> strChar <+> is '\''
182||| Recognise an integer literal (possibly with a '-' prefix)
185||| Recognise an integer literal (possibly with a '-' prefix)
183export
186export
184intLit : Lexer
187intLit : Lexer
185−intLit = (is '-' <|> Empty) <+> digits
188+intLit = opt (is '-') <+> digits
186
189
187||| A mapping from lexers to the tokens they produce.
190||| A mapping from lexers to the tokens they produce.
188||| This is a list of pairs `(Lexer, String -> tokenType)`
191||| This is a list of pairs `(Lexer, String -> tokenType)`