Compiler/lexical analyzer: Difference between revisions

m
ol example fixed
m (ol example fixed)
m (ol example fixed)
Line 6,600:
(let-parses (
(lead (get-byte-if (lambda (x) (or (<= #\a x #\z) (<= #\A x #\Z) (= x #\_)))))
(tail (get-kleenegreedy* (get-byte-if (lambda (x) (or (<= #\a x #\z) (<= #\A x #\Z) (= x #\_) (<= #\0 x #\9)))))))
(cons 'identifier (bytes->string (cons lead tail)))))
 
(define get-integer
(let-parses (
(main (get-kleenegreedy+ (get-byte-if (lambda (x) (<= #\0 x #\9))))) )
(cons 'integer (string->integer (bytes->string main)))))
 
Line 6,620:
(define get-string
(let-parses (
(_ (get-imm 34#\")) ; note: we use 34 instead of #\" only for invalid source code colorizer
(data (get-kleenegreedy* (get-any-of
(get-word "\\n" #\newline)
(get-word "\\\\" #\\) ;\"
(get-byte-if (lambda (x) (not (or (eq? x 34#\") (eq? x #\newline)))))))) ;", newline
(_ (get-imm 34#\")) ) ;"
(cons 'string (bytes->string data))))
 
Line 6,644:
(define token-parser
(let-parses (
(tokens (get-kleenegreedy+ get-token))
(_ (get-greedy* get-whitespace)))
tokens))
 
 
(define (translate source)
(let ((stream (try-parse token-parser (str-iter source) #t)))
(for-each print (car stream))
(if (null? (cdr stream))
(print 'End_of_input))))
</lang>