Skip to content

Commit

Permalink
More PEG operators
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigogribeiro committed Apr 11, 2022
1 parent 9c65485 commit 2a1002f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/test13.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#lang typed-peg/debug/parse-only

start: & "a" / "bb" +

13 changes: 11 additions & 2 deletions grammar.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(define (string->tree s)
(match s
['() (peps)]
[(cons c '()) (pchr c)]
[(cons c s1) (pcat (pchr c)
(string->tree s1))]))

Expand All @@ -32,13 +33,21 @@
[(cat) $1])
(cat [(cat term) (pcat $1 $2)]
[(term) $1])
(term [(NOT term) (pneg $2)]
(term [(prefixop term) ($1 $2)]
[(factor) $1])
(factor [(factor STAR) (pstar $1)]
(prefixop [(NOT) (lambda (e) (pneg e))]
[(AND) (lambda (e) (pneg (pneg e)))])
(factor [(factor postfix) ($2 $1)]
[(atom) $1])
(postfix [(STAR) (lambda (e) (pstar e))]
[(PLUS) (lambda (e) (pcat e (pstar e)))]
[(OPTION) (lambda (e) (pchoice e peps))])
(char-list [(CHAR) (pchr (car (string->list $1)))]
[(CHAR COMMA char-list) (pchoice $1 $3)])
(atom [(EPSILON) (peps)]
[(CHAR) (pchr (car (string->list $1)))]
[(STRING) (string->tree (string->list $1))]
[(LBRACK char-list RBRACK) $2]
[(ANY) (pany)]
[(VAR) (pvar $1)]
[(LPAREN expr RPAREN) $2])
Expand Down
23 changes: 22 additions & 1 deletion lexer.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,36 @@
(CHAR VAR STRING))

(define-empty-tokens op-tokens
(EOF OR LPAREN RPAREN STAR NOT SEMI EPSILON ARROW START ANY))
(EOF OR
LPAREN
RPAREN
STAR
NOT
SEMI
EPSILON
ARROW
START
ANY
PLUS
OPTION
AND
LBRACK
RBRACK
COMMA))

(define next-token
(lexer-src-pos
[(eof) (token-EOF)]
[(:+ whitespace #\newline) (return-without-pos (next-token input-port))]
["." (token-ANY)]
["," (token-COMMA)]
["[" (token-LBRACK)]
["]" (token-RBRACK)]
["/" (token-OR)]
["+" (token-PLUS)]
["?" (token-OPTION)]
["*" (token-STAR)]
["&" (token-AND)]
["<--" (token-ARROW)]
["!" (token-NOT)]
[";" (token-SEMI)]
Expand Down

0 comments on commit 2a1002f

Please sign in to comment.