factor: fix EBNF: syntax.

locals-and-roots
Doug Coleman 2016-06-22 17:40:56 -07:00
parent c3c923749c
commit 542eecab1c
21 changed files with 44 additions and 45 deletions

View File

@ -34,7 +34,7 @@ TUPLE: sub < operator ; C: <sub> sub ;
TUPLE: mul < operator ; C: <mul> mul ; TUPLE: mul < operator ; C: <mul> mul ;
TUPLE: div < operator ; C: <div> div ; TUPLE: div < operator ; C: <div> div ;
: expr-ast ( string -- obj ) EBNF{{ EBNF: expr-ast [=[
spaces = [\n\t ]* spaces = [\n\t ]*
digit = [0-9] digit = [0-9]
number = (digit)+ => [[ string>number ]] number = (digit)+ => [[ string>number ]]
@ -51,7 +51,7 @@ exp = exp:a spaces "+" fac:b => [[ a b <add> ]]
| fac | fac
main = exp:e spaces !(.) => [[ e ]] main = exp:e spaces !(.) => [[ e ]]
}} ; ]=] ;
GENERIC: eval-ast ( ast -- result ) ; GENERIC: eval-ast ( ast -- result ) ;

View File

@ -13,7 +13,7 @@ ERROR: bad-number str ;
: check-number ( str -- n ) : check-number ( str -- n )
>string dup string>number [ ] [ bad-number ] ?if ; >string dup string>number [ ] [ bad-number ] ?if ;
: parse-smalltalk ( string -- obj ) EBNF{{ EBNF: parse-smalltalk [=[
Character = . Character = .
WhitespaceCharacter = [ \t\n\r] WhitespaceCharacter = [ \t\n\r]
@ -225,4 +225,4 @@ End = !(.)
Program = TopLevelForm End Program = TopLevelForm End
}} ; ]=] ;

View File

@ -5,7 +5,7 @@ combinators.short-circuit accessors math.parser quoting
locals ; locals ;
IN: db.postgresql.errors IN: db.postgresql.errors
: parse-postgresql-sql-error ( string -- obj ) EBNF{{ EBNF: parse-postgresql-sql-error [=[
Error = "ERROR:" [ ]+ Error = "ERROR:" [ ]+
@ -35,7 +35,7 @@ UnknownError = .* => [[ >string <sql-unknown-error> ]]
PostgresqlSqlError = (TableError | DatabaseError | FunctionError | SyntaxError | UnknownError) PostgresqlSqlError = (TableError | DatabaseError | FunctionError | SyntaxError | UnknownError)
}} ; ]=] ;
TUPLE: parse-postgresql-location column line text ; TUPLE: parse-postgresql-location column line text ;

View File

@ -14,7 +14,7 @@ SINGLETONS: table-exists table-missing ;
{ table-exists [ <sql-table-exists> ] } { table-exists [ <sql-table-exists> ] }
} case ; } case ;
: parse-sqlite-sql-error ( string -- obj ) EBNF{{ EBNF: parse-sqlite-sql-error [=[
TableMessage = " already exists" => [[ table-exists ]] TableMessage = " already exists" => [[ table-exists ]]
@ -25,4 +25,4 @@ SqliteError =
=> [[ table >string <sql-table-missing> ]] => [[ table >string <sql-table-missing> ]]
| .*:error | .*:error
=> [[ error >string <unparsed-sqlite-error> ]] => [[ error >string <unparsed-sqlite-error> ]]
}} ; ]=] ;

View File

@ -4,12 +4,12 @@ IN: compiler.tests.peg-regression-2
GENERIC: <times> ( times -- term' ) ; GENERIC: <times> ( times -- term' ) ;
M: string <times> ; M: string <times> ;
: parse-regexp ( string -- obj ) EBNF{{ EBNF: parse-regexp [=[
Times = .* => [[ "foo" ]] Times = .* => [[ "foo" ]]
Regexp = Times:t => [[ t <times> ]] Regexp = Times:t => [[ t <times> ]]
}} ; ]=] ;
[ "foo" ] [ "a" parse-regexp ] unit-test [ "foo" ] [ "a" parse-regexp ] unit-test

View File

@ -4,7 +4,7 @@ USING: accessors assocs combinators combinators.short-circuit
effects kernel make math math.parser multiline namespaces parser effects kernel make math math.parser multiline namespaces parser
peg peg.parsers quotations sequences sequences.deep splitting peg peg.parsers quotations sequences sequences.deep splitting
stack-checker strings strings.parser summary unicode stack-checker strings strings.parser summary unicode
vocabs.parser words ; vocabs.parser words lexer ;
FROM: vocabs.parser => search ; FROM: vocabs.parser => search ;
FROM: peg.search => replace ; FROM: peg.search => replace ;
IN: peg.ebnf IN: peg.ebnf
@ -558,7 +558,7 @@ SYNTAX: \ EBNF((
suffix! \ call suffix! reset-tokenizer ; suffix! \ call suffix! reset-tokenizer ;
SYNTAX: \ EBNF: SYNTAX: \ EBNF:
reset-tokenizer scan-new-word dup "EBNF;" parse-multiline-string reset-tokenizer scan-new-word dup scan-object ";" expect
ebnf>quot swapd ebnf>quot swapd
( input -- ast ) define-declared "ebnf-parser" set-word-prop ( input -- ast ) define-declared "ebnf-parser" set-word-prop
reset-tokenizer ; reset-tokenizer ;

View File

@ -49,7 +49,7 @@ TUPLE: brainfuck pointer memory ;
: compose-all ( seq -- quot ) : compose-all ( seq -- quot )
[ ] [ compose ] reduce ; [ ] [ compose ] reduce ;
: parse-brainfuck ( string -- obj ) EBNF{{ EBNF: parse-brainfuck [=[
inc-ptr = (">")+ => [[ length '[ _ (>) ] ]] inc-ptr = (">")+ => [[ length '[ _ (>) ] ]]
dec-ptr = ("<")+ => [[ length '[ _ (<) ] ]] dec-ptr = ("<")+ => [[ length '[ _ (<) ] ]]
@ -66,7 +66,7 @@ loop = "[" {loop|ops}+ "]" => [[ second compose-all '[ [ (?) ] _ while ] ]]
code = (loop|ops|unknown)* => [[ compose-all ]] code = (loop|ops|unknown)* => [[ compose-all ]]
}} ; ]=] ;
PRIVATE> PRIVATE>

View File

@ -37,7 +37,7 @@ PRIVATE<
ERROR: unknown-printf-directive ; ERROR: unknown-printf-directive ;
: parse-printf ( string -- obj ) EBNF{{ EBNF: parse-printf [=[
zero = "0" => [[ char: 0 ]] zero = "0" => [[ char: 0 ]]
char = "'" (.) => [[ second ]] char = "'" (.) => [[ second ]]
@ -90,7 +90,7 @@ plain-text = (!("%").)+ => [[ >string ]]
text = (formats|plain-text)* => [[ ]] text = (formats|plain-text)* => [[ ]]
}} ; ]=] ;
: printf-quot ( format-string -- format-quot n ) : printf-quot ( format-string -- format-quot n )
parse-printf [ [ callable? ] count ] keep [ parse-printf [ [ callable? ] count ] keep [

View File

@ -12,7 +12,7 @@ IN: globs
: wild-path-separator ( -- sep ) : wild-path-separator ( -- sep )
os windows? R{{ [^\\/\\][\\/\\]|[^\\/\\]}} R{{ [^\\/][\\/]|[^\\/]}} ? ; foldable os windows? R{{ [^\\/\\][\\/\\]|[^\\/\\]}} R{{ [^\\/][\\/]|[^\\/]}} ? ; foldable
: <glob> ( string -- obj ) EBNF{{ EBNF: <glob> [=[
Character = "\\" .:c => [[ c 1string <literal> ]] Character = "\\" .:c => [[ c 1string <literal> ]]
| !(","|"}") . => [[ 1string <literal> ]] | !(","|"}") . => [[ 1string <literal> ]]
@ -45,7 +45,7 @@ End = !(.)
Main = Concatenation End Main = Concatenation End
}} ; ]=] ;
: glob-matches? ( input glob -- ? ) : glob-matches? ( input glob -- ? )
[ >case-fold ] bi@ <glob> matches? ; [ >case-fold ] bi@ <glob> matches? ;

View File

@ -52,7 +52,7 @@ ERROR: bad-vector-length seq n ;
[ bad-vector-length ] [ bad-vector-length ]
} case ; } case ;
: parse-gml ( string -- obj ) EBNF{{ EBNF: parse-gml [=[
Letter = [a-zA-Z] Letter = [a-zA-Z]
Digit = [0-9] Digit = [0-9]
@ -124,4 +124,4 @@ Tokens = Token* => [[ [ comment? ] reject ]]
Program = Tokens Spaces !(.) => [[ parse-proc ]] Program = Tokens Spaces !(.) => [[ parse-proc ]]
}} ; ]=] ;

View File

@ -4,7 +4,7 @@ USING: infix.ast infix.tokenizer kernel math peg.ebnf sequences
strings vectors ; strings vectors ;
IN: infix.parser IN: infix.parser
: parse-infix ( string -- obj ) EBNF{{ EBNF: parse-infix [=[
Number = . ?[ ast-number? ]? Number = . ?[ ast-number? ]?
Identifier = . ?[ string? ]? Identifier = . ?[ string? ]?
Array = Identifier:i "[" Sum:s "]" => [[ i s ast-array boa ]] Array = Identifier:i "[" Sum:s "]" => [[ i s ast-array boa ]]
@ -29,7 +29,7 @@ Sum = Sum:s ("+"|"-"):op Product:p => [[ s p op ast-op boa ]]
End = !(.) End = !(.)
Expression = Sum End Expression = Sum End
}} ; ]=] ;
: build-infix-ast ( string -- ast ) : build-infix-ast ( string -- ast )
tokenize-infix parse-infix ; tokenize-infix parse-infix ;

View File

@ -4,7 +4,7 @@ USING: infix.ast kernel peg peg.ebnf math.parser sequences
strings ; strings ;
IN: infix.tokenizer IN: infix.tokenizer
: tokenize-infix ( string -- obj ) EBNF{{ EBNF: tokenize-infix [=[
Letter = [a-zA-Z] Letter = [a-zA-Z]
Digit = [0-9] Digit = [0-9]
Digits = Digit+ Digits = Digit+
@ -21,4 +21,4 @@ Special = [+*/%(),] | "-" => [[ char: - ]]
Tok = Spaces (Name | Number | Special ) Tok = Spaces (Name | Number | Special )
End = !(.) End = !(.)
Toks = Tok* Spaces End Toks = Tok* Spaces End
}} ; ]=] ;

View File

@ -4,7 +4,7 @@ USING: kernel arrays strings math.parser sequences
peg peg.ebnf peg.parsers memoize math accessors ; peg peg.ebnf peg.parsers memoize math accessors ;
IN: peg.expr IN: peg.expr
: expr ( string -- obj ) EBNF{{ EBNF: expr [=[
number = ([0-9])+ => [[ string>number ]] number = ([0-9])+ => [[ string>number ]]
value = number value = number
| ("(" exp ")") => [[ second ]] | ("(" exp ")") => [[ second ]]
@ -16,4 +16,4 @@ fac = fac "*" value => [[ first3 nip * ]]
exp = exp "+" fac => [[ first3 nip + ]] exp = exp "+" fac => [[ first3 nip + ]]
| exp "-" fac => [[ first3 nip - ]] | exp "-" fac => [[ first3 nip - ]]
| fac | fac
}} ; ]=] ;

View File

@ -18,7 +18,7 @@ IN: peg.javascript.parser
! This operates a character at a time. Using this 'nl' in the parser ! This operates a character at a time. Using this 'nl' in the parser
! allows us to detect newlines when we need to for the semicolon ! allows us to detect newlines when we need to for the semicolon
! insertion rule, but ignore it in all other places. ! insertion rule, but ignore it in all other places.
: javascript ( string -- obj ) EBNF{{ EBNF: javascript [=[
tokenizer = default tokenizer = default
nl = "\r\n" | "\n" nl = "\r\n" | "\n"
@ -197,4 +197,4 @@ SrcElem = "function" Name:n FuncRest:f => [[ n f a
| Stmt | Stmt
SrcElems = SrcElem* => [[ ast-begin boa ]] SrcElems = SrcElem* => [[ ast-begin boa ]]
TopLevel = SrcElems Spaces TopLevel = SrcElems Spaces
}} ; ]=] ;

View File

@ -8,7 +8,7 @@ IN: peg.javascript.tokenizer
USE: prettyprint USE: prettyprint
: tokenize-javascript ( string -- obj ) EBNF{{ EBNF: tokenize-javascript [=[
Letter = [a-zA-Z] Letter = [a-zA-Z]
Digit = [0-9] Digit = [0-9]
Digits = Digit+ Digits = Digit+
@ -87,4 +87,4 @@ Special = "(" | ")" | "{" | "}" | "[" | "]" | "," |
| "^" | "^"
Tok = Spaces (Name | Keyword | Number | Str | RegExp | Special ) Tok = Spaces (Name | Keyword | Number | Str | RegExp | Special )
Toks = Tok* Spaces Toks = Tok* Spaces
}} ; ]=] ;

View File

@ -6,7 +6,7 @@ IN: peg.pl0
! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0 ! Grammar for PL/0 based on http://en.wikipedia.org/wiki/PL/0
: pl0 ( string -- obj ) EBNF{{ EBNF: pl0 [=[
block = { "CONST" ident "=" number { "," ident "=" number }* ";" }? block = { "CONST" ident "=" number { "," ident "=" number }* ";" }?
{ "VAR" ident { "," ident }* ";" }? { "VAR" ident { "," ident }* ";" }?
@ -24,4 +24,4 @@ factor = ident | number | "(" expression ")"
ident = (([a-zA-Z])+) => [[ >string ]] ident = (([a-zA-Z])+) => [[ >string ]]
number = ([0-9])+ => [[ string>number ]] number = ([0-9])+ => [[ string>number ]]
program = { block "." } program = { block "." }
}} ; ]=] ;

View File

@ -126,7 +126,7 @@ ERROR: nonexistent-option name ;
! add greedy and nongreedy forms of matching ! add greedy and nongreedy forms of matching
! (once it's all implemented) ! (once it's all implemented)
: parse-regexp ( string -- obj ) EBNF{{ EBNF: parse-regexp [=[
CharacterInBracket = !("}") Character CharacterInBracket = !("}") Character
@ -217,4 +217,4 @@ Alternation = Concatenation:c ("|" Concatenation)*:a
End = !(.) End = !(.)
Main = Alternation End Main = Alternation End
}} ; ]=] ;

View File

@ -41,7 +41,7 @@ TUPLE: factor-expr expr ;
: ast>factor-expr ( ast -- obj ) : ast>factor-expr ( ast -- obj )
second >string factor-expr boa ; second >string factor-expr boa ;
: expr ( string -- obj ) EBNF{{ EBNF: expr [=[
space = " " space = " "
@ -89,4 +89,4 @@ pipeline = _ command _ (in-file)? _ "|" _ (command _ "|" _)* command _ (to-file
submission = (pipeline | basic) submission = (pipeline | basic)
}} ; ]=] ;

View File

@ -3,11 +3,11 @@
USING: peg.ebnf strings ; USING: peg.ebnf strings ;
IN: simple-tokenizer IN: simple-tokenizer
: tokenize ( string -- obj ) EBNF{{ EBNF: tokenize [=[
space = [ \t\n\r] space = [ \t\n\r]
escaped-char = "\\" .:ch => [[ ch ]] escaped-char = "\\" .:ch => [[ ch ]]
quoted = '"' (escaped-char | [^"])*:a '"' => [[ a ]] quoted = '"' (escaped-char | [^"])*:a '"' => [[ a ]]
unquoted = (escaped-char | [^ \t\n\r"])+ unquoted = (escaped-char | [^ \t\n\r"])+
argument = (quoted | unquoted) => [[ >string ]] argument = (quoted | unquoted) => [[ >string ]]
command = space* (argument:a space* => [[ a ]])+:c !(.) => [[ c ]] command = space* (argument:a space* => [[ a ]])+:c !(.) => [[ c ]]
}} ; ]=] ;

View File

@ -16,8 +16,7 @@ XML-NS: inkscape-name http://www.inkscape.org/namespaces/inkscape ;
: degrees ( deg -- rad ) pi * 180.0 / ; : degrees ( deg -- rad ) pi * 180.0 / ;
: svg-transform>affine-transform ( string -- obj ) EBNF: svg-transform>affine-transform [=[
EBNF{{
transforms = transforms =
transform:m comma-wsp+ transforms:n => [[ m n a. ]] transform:m comma-wsp+ transforms:n => [[ m n a. ]]
@ -77,7 +76,7 @@ wsp = [ \t\r\n]
transform-list = wsp* transforms?:t wsp* transform-list = wsp* transforms?:t wsp*
=> [[ t [ identity-transform ] unless* ]] => [[ t [ identity-transform ] unless* ]]
}} ; ]=] ;
: tag-transform ( tag -- transform ) : tag-transform ( tag -- transform )
"transform" svg-name attr svg-transform>affine-transform ; "transform" svg-name attr svg-transform>affine-transform ;
@ -96,7 +95,7 @@ TUPLE: elliptical-arc radii x-axis-rotation large-arc? sweep? p relative? ;
: (set-relative) ( args rel -- args ) : (set-relative) ( args rel -- args )
'[ [ _ >>relative? drop ] each ] keep ; '[ [ _ >>relative? drop ] each ] keep ;
: svg-path>array ( string -- obj ) EBNF{{ EBNF: svg-path>array [=[
moveto-drawto-command-groups = moveto-drawto-command-groups =
moveto-drawto-command-group:first wsp* moveto-drawto-command-groups:rest moveto-drawto-command-group:first wsp* moveto-drawto-command-groups:rest
@ -219,7 +218,7 @@ wsp = [ \t\r\n]
svg-path = wsp* moveto-drawto-command-groups?:x wsp* => [[ x ]] svg-path = wsp* moveto-drawto-command-groups?:x wsp* => [[ x ]]
}} ; ]=] ;
: tag-d ( tag -- d ) : tag-d ( tag -- d )
"d" svg-name attr svg-path>array ; "d" svg-name attr svg-path>array ;

View File

@ -35,7 +35,7 @@ M: url >url ;
PRIVATE< PRIVATE<
: parse-url ( string -- obj ) EBNF{{ EBNF: parse-url [=[
protocol = [a-z+]+ => [[ url-decode ]] protocol = [a-z+]+ => [[ url-decode ]]
username = [^/:@#?]+ => [[ url-decode ]] username = [^/:@#?]+ => [[ url-decode ]]
@ -57,7 +57,7 @@ url = (((protocol "://") => [[ first ]] auth hostname)
("?" query => [[ second ]])? ("?" query => [[ second ]])?
("#" anchor => [[ second ]])? ("#" anchor => [[ second ]])?
}} ; ]=] ;
PRIVATE> PRIVATE>