From 1570e819a875ac0e7f80857f0793784984db687d Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Fri, 28 Nov 2014 20:36:42 -0400 Subject: + Added single-line comments. + Added multi-line comments, with nesting. --- src/example/test1.lang | 4 ++++ src/lang/lexer.clj | 55 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/example/test1.lang b/src/example/test1.lang index 43f3111fa..f805b1085 100644 --- a/src/example/test1.lang +++ b/src/example/test1.lang @@ -1,16 +1,20 @@ (* 5 6) +## My first function definition! (def (repeat n val) (if (<=' n 0) (#Nil []) (#Cons [val (repeat (-' n 1) val)]))) +## Testing one, two, three... (repeat 5 5) (def (fold f init inputs) (case inputs + #( Outer comment #( Inner comment )# )# (#Nil []) init (#Cons [head tail]) (fold f (f init head) tail))) +## It's alive! (fold * 1 (repeat 5 5)) diff --git a/src/lang/lexer.clj b/src/lang/lexer.clj index bbb92da95..a51330e09 100644 --- a/src/lang/lexer.clj +++ b/src/lang/lexer.clj @@ -22,15 +22,24 @@ ;; [Lexers] (def ^:private lex-white-space (lex-regex #"^(\s+)")) +(def lex-forms + (exec [forms (repeat-m lex-form)] + (return (filter #(match % + [::comment _] + false + _ + true) + forms)))) + (def ^:private lex-list (exec [_ (lex-str "(") - members (repeat-m lex-form) + members lex-forms _ (lex-str ")")] (return [::list members]))) (def ^:private lex-tuple (exec [_ (lex-str "[") - members (repeat-m lex-form) + members lex-forms _ (lex-str "]")] (return [::tuple members]))) @@ -44,6 +53,42 @@ ^:private lex-int ::int #"^(0|[1-9][0-9]*)" ^:private lex-ident ::ident +ident-re+) +(def ^:private lex-single-line-comment + (exec [_ (lex-str "##") + comment (lex-regex #"^([^\n]*)") + _ (lex-regex #"^(\n?)") + ;; :let [_ (prn 'comment comment)] + ] + (return [::comment comment]))) + +(def ^:private lex-multi-line-comment + (exec [_ (lex-str "#(") + ;; :let [_ (prn 'OPEN)] + ;; comment (lex-regex #"^(#\(.*\)#)") + comment (try-all-m [(lex-regex #"^((?!#\().)*?(?=\)#)") + (exec [pre (lex-regex #"^(.+?(?=#\())") + ;; :let [_ (prn 'PRE pre)] + [_ inner] lex-multi-line-comment + ;; :let [_ (prn 'INNER inner)] + post (lex-regex #"^(.+?(?=\)#))") + ;:let [_ (prn 'POST post)] + ] + (return (str pre "#(" inner ")#" post)))]) + ;; :let [_ (prn 'COMMENT comment)] + _ (lex-str ")#") + ;; :let [_ (prn 'CLOSE)] + ;; :let [_ (prn 'multi-comment comment)] + ] + (return [::comment comment]))) + +;; #"^(.*?!(#\()).*#\)" + +;; ;; UP TO #( +;; #"^.+?(?=#\()" + +;; ;; UP TO )# +;; #"^.+?(?=\)#)" + (def ^:private lex-tag (exec [_ (lex-str "#") token (lex-regex +ident-re+)] @@ -55,13 +100,15 @@ lex-ident lex-tag lex-list - lex-tuple]) + lex-tuple + lex-single-line-comment + lex-multi-line-comment]) _ (try-m lex-white-space)] (return form))) ;; [Interface] (defn lex [text] - (match ((repeat-m lex-form) text) + (match (lex-forms text) [::&util/ok [?state ?forms]] (if (empty? ?state) ?forms -- cgit v1.2.3