From 2a1002fa94cefb4416f9a3c29f5af1f2fd239d36 Mon Sep 17 00:00:00 2001 From: Rodrigo Ribeiro Date: Mon, 11 Apr 2022 19:59:34 -0300 Subject: [PATCH] More PEG operators --- examples/test13.rkt | 4 ++++ grammar.rkt | 13 +++++++++++-- lexer.rkt | 23 ++++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 examples/test13.rkt diff --git a/examples/test13.rkt b/examples/test13.rkt new file mode 100644 index 0000000..7973bcf --- /dev/null +++ b/examples/test13.rkt @@ -0,0 +1,4 @@ +#lang typed-peg/debug/parse-only + +start: & "a" / "bb" + + diff --git a/grammar.rkt b/grammar.rkt index 4da223e..e6e1195 100644 --- a/grammar.rkt +++ b/grammar.rkt @@ -10,6 +10,7 @@ (define (string->tree s) (match s ['() (peps)] + [(cons c '()) (pchr c)] [(cons c s1) (pcat (pchr c) (string->tree s1))])) @@ -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]) diff --git a/lexer.rkt b/lexer.rkt index 2539019..e61b71e 100644 --- a/lexer.rkt +++ b/lexer.rkt @@ -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)]