diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/compiler/default/syntax.lux | 63 | ||||
-rw-r--r-- | stdlib/test/test/lux/compiler/default/syntax.lux | 36 |
2 files changed, 5 insertions, 94 deletions
diff --git a/stdlib/source/lux/compiler/default/syntax.lux b/stdlib/source/lux/compiler/default/syntax.lux index 6a52687ec..3b3b3e411 100644 --- a/stdlib/source/lux/compiler/default/syntax.lux +++ b/stdlib/source/lux/compiler/default/syntax.lux @@ -132,7 +132,7 @@ ## Single-line comments can start anywhere, but only go up to the ## next new-line. -(def: (single-line-comment^ where) +(def: (comment^ where) (-> Cursor (Lexer Cursor)) (do p.Monad<Parser> [_ (l.this ..single-line-comment-marker) @@ -142,67 +142,6 @@ (update@ #.line inc) (set@ #.column 0))))) -## This is just a helper parser to find text which doesn't run into -## any special character sequences for multi-line comments. -(def: multi-line-comment-start^ (l.this (format ..sigil open-form))) -(def: multi-line-comment-end^ (l.this (format close-form ..sigil))) - -(def: multi-line-comment-bound^ - (Lexer Any) - ($_ p.either - ..new-line^ - ..multi-line-comment-start^ - ..multi-line-comment-end^)) - -## Multi-line comments are bounded by #( these delimiters, #(and, they may -## also be nested)# )#. -## Multi-line comment syntax must be balanced. -## That is, any nested comment must have matched delimiters. -## Unbalanced comments ought to be rejected as invalid code. -(def: (multi-line-comment^ where) - (-> Cursor (Lexer Cursor)) - (do p.Monad<Parser> - [_ ..multi-line-comment-start^] - (loop [where (update@ #.column (n/+ 2) where)] - ($_ p.either - ## These are normal chunks of commented text. - (do @ - [chunk (l.many! (l.not! multi-line-comment-bound^))] - (recur (|> where - (update@ #.column (n/+ (get@ #l.distance chunk)))))) - ## This is a special rule to handle new-lines within - ## comments properly. - (do @ - [_ ..new-line^] - (recur (|> where - (update@ #.line inc) - (set@ #.column 0)))) - ## This is the rule for handling nested sub-comments. - ## Ultimately, the whole comment is just treated as text - ## (the comment must respect the syntax structure, but the - ## output produced is just a block of text). - ## That is why the sub-comment is covered in delimiters - ## and then appended to the rest of the comment text. - (do @ - [sub-where (multi-line-comment^ where)] - (recur sub-where)) - ## Finally, this is the rule for closing the comment. - (do @ - [_ ..multi-line-comment-end^] - (wrap (update@ #.column (n/+ 2) where))) - )))) - -## This is the only parser that should be used directly by other -## parsers, since all comments must be treated as either being -## single-line or multi-line. -## That is, there is no syntactic rule prohibiting one type of comment -## from being used in any situation (alternatively, forcing one type -## of comment to be the only usable one). -(def: (comment^ where) - (-> Cursor (Lexer Cursor)) - (p.either (single-line-comment^ where) - (multi-line-comment^ where))) - ## To simplify parsing, I remove any left-padding that a Code token ## may have prior to parsing the token itself. ## Left-padding is assumed to be either white-space or a comment. diff --git a/stdlib/test/test/lux/compiler/default/syntax.lux b/stdlib/test/test/lux/compiler/default/syntax.lux index a9baa546c..1bcb9dad8 100644 --- a/stdlib/test/test/lux/compiler/default/syntax.lux +++ b/stdlib/test/test/lux/compiler/default/syntax.lux @@ -114,25 +114,16 @@ (def: comment-text^ (r.Random Text) - (let [char-gen (|> r.nat (r.filter (function (_ value) - (not (or (text.space? value) - (n/= (char "#") value) - (n/= (char "(") value) - (n/= (char ")") value))))))] + (let [char-gen (|> r.nat (r.filter (|>> (n/= (char "\n")) not)))] (do r.Monad<Random> [size (|> r.nat (r/map (n/% 20)))] (r.text char-gen size)))) (def: comment^ (r.Random Text) - (r.either (do r.Monad<Random> - [comment comment-text^] - (wrap (format "## " comment "\n"))) - (r.rec (function (_ nested^) - (do r.Monad<Random> - [comment (r.either comment-text^ - nested^)] - (wrap (format "#( " comment " )#"))))))) + (do r.Monad<Random> + [comment comment-text^] + (wrap (format "## " comment "\n")))) (context: "Multi-line text & comments." (<| (seed 12137892244981970631) @@ -189,23 +180,4 @@ (#e.Success [_ parsed]) (:: code.Equivalence<Code> = parsed sample))) - (test "Will reject unbalanced multi-line comments." - (and (case (&.read "" (dict.new text.Hash<Text>) - [default-cursor 0 - (format "#(" "#(" unbalanced-comment ")#" - (code.to-text sample))]) - (#e.Error error) - #1 - - (#e.Success [_ parsed]) - #0) - (case (&.read "" (dict.new text.Hash<Text>) - [default-cursor 0 - (format "#(" unbalanced-comment ")#" ")#" - (code.to-text sample))]) - (#e.Error error) - #1 - - (#e.Success [_ parsed]) - #0))) )))) |