summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2019-11-26 19:25:03 +0100
committertomsmeding <tom.smeding@gmail.com>2019-11-26 19:25:03 +0100
commit650a88d875acd4ec7c9cc31dac29fb447a987d31 (patch)
tree405e303354e4418bce777da3a764a3926c733cfc
parent5af875717bbbb3892fbe3b92783804a3b62841a6 (diff)
Parse lispparser.lisp with lispparser.lisp
-rw-r--r--tests/lispparser.lisp27
1 files changed, 23 insertions, 4 deletions
diff --git a/tests/lispparser.lisp b/tests/lispparser.lisp
index b6dfb67..df1ed32 100644
--- a/tests/lispparser.lisp
+++ b/tests/lispparser.lisp
@@ -26,6 +26,7 @@
(= clr 'green) "32"
(= clr 'yellow) "33"
(= clr 'blue) "34"
+ (= clr 'cyan) "36"
(error "Unknown color in ansi-color" clr))))
(concat (concat "\x1B[" (concat style "m")) (concat str "\x1B[0m"))))
@@ -119,7 +120,7 @@
(parse-string-contents rest (concat yet ch)))))
; deftype token = ('tag <field>), where the type of <field> depends on the tag
-; ('open '()) ('close '()) ('quote '()) ('string "text") ('number 123) ('name "name")
+; ('open '()) ('close '()) ('quote '()) ('string "text") ('number 123) ('name "name") ('include "path")
; string -> (token string[rest])
(define next-token (str)
(let ((ch (substr 0 1 str))
@@ -137,6 +138,16 @@
(list (list 'close '()) rest)
(= ch "'")
(list (list 'quote '()) rest)
+ (= ch "#")
+ (if (= (substr 0 7 rest) "include")
+ (let ((pair (next-token (substr 7 -1 rest))))
+ (cond
+ (null? pair)
+ (error "Expected path after #include")
+ (= (car (car pair)) 'string)
+ (list (list 'include (cadr (car pair))) (cadr pair))
+ (error "Expected string after #include")))
+ (error "Unknown preprocessor directive after '#'"))
(= ch "\"")
(let ((pair (parse-string-contents rest ""))
(text (car pair))
@@ -179,6 +190,8 @@
(ansi-color 'blue (number->string field))
(= tag 'name)
field
+ (= tag 'include)
+ (concat (concat (ansi-color 'cyan "#include") " ") (ansi-color 'yellow (concat "\"" (concat (string-escape field) "\""))))
(error "Invalid token tag in pretty-token" tag))))
; tokens -> string
@@ -202,7 +215,7 @@
(declare parse-sexpr)
; deftype item = ('tag <field>), where the type of <field> depends on the tag
-; ('string "text") ('number 123) ('name "name") ('quote item) ('sexpr (list item...))
+; ('string "text") ('number 123) ('name "name") ('quote item) ('sexpr (list item...)) ('include "path")
; tokens -> (item tokens[rest])
(define parse-item (logged-token-fn "parse-item" (lambda (tokens)
(if (null? tokens)
@@ -222,6 +235,8 @@
(list (car tokens) (cdr tokens))
(= tag 'name)
(list (car tokens) (cdr tokens))
+ (= tag 'include)
+ (list (car tokens) (cdr tokens))
(error "Invalid token tag?")))))))
; tokens -> (list[items] tokens[rest])
@@ -276,6 +291,8 @@
(pretty-token ast)
(= (car ast) 'name)
(pretty-token ast)
+ (= (car ast) 'include)
+ (pretty-token ast)
(error "Unrecognised AST type in pretty-ast-compact" (car ast))))
; ast -> string
@@ -317,10 +334,12 @@
(pretty-token ast)
(= (car ast) 'name)
(pretty-token ast)
+ (= (car ast) 'include)
+ (pretty-token ast)
(error "Unrecognised AST type in pretty-ast" (car ast)))))
-(let ((tokens (tokenise (read-file "tests/stdlib.lisp"))))
+(let ((tokens (tokenise (read-file "tests/lispparser.lisp"))))
(do
- (print (pretty-token-list tokens))
+ ; (print (pretty-token-list tokens))
; (print (parse-program tokens))
(print (pretty-ast (parse-program tokens)))))