1 (in-package #:cl-nl.lexer)
3 ; I played around with using #'read for netlogo code, which would have been neat.
4 ; However, it provides too many instances where people could inject CL code
5 ; and I wanted to prevent that. In the end, writing my own lexer became kind of fun.
7 (defvar *state* :initial)
8 (defvar *states* :extension-literal)
9 (defparameter *lexes* nil)
11 (defun as-symbol (text) (intern (string-upcase text) :keyword))
13 (defmacro deflex (state match &optional func)
17 ((,scanner (when (stringp ,match) (cl-ppcre:create-scanner ,match))))
24 (and (symbolp text) (eql text ,match))
27 (multiple-value-bind (start end) (cl-ppcre:scan ,scanner text)
28 (and start end (= 0 start) (/= 0 end)))))))
29 (lambda (text) (second (multiple-value-list (cl-ppcre:scan ,scanner text))))
30 ,(or func #'as-symbol))
36 ((lex (find-if (lambda (f) (funcall f *state* :eof)) *lexes* :key #'car)))
37 (when lex (list (funcall (third lex) :eof))))
39 ((lex (find-if (lambda (f) (funcall f *state* text)) *lexes* :key #'car)))
40 (when (not lex) (error "Can't lex this: ~S" text))
42 ((val (funcall (third lex) (subseq text 0 (funcall (cadr lex) text)))))
44 (cons val (lex (subseq text (funcall (cadr lex) text))))
45 (lex (subseq text (funcall (cadr lex) text))))))))
47 (defun set-state (new-state)
48 (setf *state* new-state))
50 ; This part is the actual netlogo spec
52 (defvar *string-text* "(\\\"|\\r|\\n|\\t|\\\\|\\[^\"]|[^\r\n\"\\])*")
53 (defvar *nonnewline_white_space_char* "[ \\t\\b\\012]")
54 (defvar *letter* "\\w")
55 (defvar *digit* "\\d")
56 ;(defparameter *identifier-char* "[\\w\\d_\\.?=\*!<>:#\+/%\$\^\'&-]")
57 (defvar *identifier-char* "[\\w\\d]")
59 ;(defvar *extension-literal-depth* 0)
60 ;(defstruct extension-literal text)
62 ;(deflex :initial "{{"
64 ; (set-state :extension-literal)
68 ;(deflex :extension-literal "}}"
70 ; (if (= 0 *extension-literal-depth*)
71 ; (progn (set-state :initial) text)
72 ; (progn (decf *extension-literal-depth*) (as-symbol text)))))
74 ;(deflex :extension-literal "{{"
75 ; (lambda (text) (incf *extension-literal-depth*) text))
77 ;(deflex :extension-literal "\n|\r" (lambda () (error "End of line reached unexpectedly")))
78 ;(deflex :extension-literal :eof (lambda () (error "end of file reached unexpectedly")))
79 ;(deflex :extension-literal ".")
81 (deflex :initial "[,\\{\\}\\[\\]\\(\\)]" #'as-symbol)
82 (deflex :initial *nonnewline_white_space_char* (constantly nil))
83 (deflex :initial "\\n|\\r" (constantly nil))
84 ;(deflex :initial ";.*[\n\r]?" nil)
85 (deflex :initial (format nil "-?\.?[0-9]~A*" *identifier-char*)
90 ((*readtable* (copy-readtable nil))
92 (read-from-string text))))
93 (if (numberp num?) num? (error "Invalid number")))))
95 (deflex :initial (format nil "~A*" *identifier-char*) #'as-symbol)
96 ;(deflex :initial (format nil "\"~A*\"" *string-text*))
97 ;(deflex :initial (format nil "\"~A*" *string-text*) (lambda (text) (error "Closing double quote is missing")))
98 ;(deflex :initial "." (lambda (text) (error "Non standard character is not allowed")))