2007-11-27 00:13:36 -05:00
|
|
|
! Copyright (C) 2007 Chris Double.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2007-11-27 17:07:17 -05:00
|
|
|
USING: kernel parser words arrays strings math.parser sequences namespaces peg ;
|
2007-11-27 00:13:36 -05:00
|
|
|
IN: peg.ebnf
|
|
|
|
|
|
|
|
TUPLE: ebnf-non-terminal symbol ;
|
|
|
|
TUPLE: ebnf-terminal symbol ;
|
|
|
|
TUPLE: ebnf-choice options ;
|
2007-11-27 16:28:28 -05:00
|
|
|
TUPLE: ebnf-sequence elements ;
|
|
|
|
TUPLE: ebnf-rule symbol elements ;
|
2007-11-27 17:25:34 -05:00
|
|
|
TUPLE: ebnf rules ;
|
2007-11-27 00:13:36 -05:00
|
|
|
|
|
|
|
C: <ebnf-non-terminal> ebnf-non-terminal
|
|
|
|
C: <ebnf-terminal> ebnf-terminal
|
|
|
|
C: <ebnf-choice> ebnf-choice
|
2007-11-27 16:28:28 -05:00
|
|
|
C: <ebnf-sequence> ebnf-sequence
|
|
|
|
C: <ebnf-rule> ebnf-rule
|
2007-11-27 17:25:34 -05:00
|
|
|
C: <ebnf> ebnf
|
2007-11-27 00:13:36 -05:00
|
|
|
|
|
|
|
GENERIC: ebnf-compile ( ast -- quot )
|
|
|
|
|
|
|
|
M: ebnf-terminal ebnf-compile ( ast -- quot )
|
|
|
|
[
|
|
|
|
ebnf-terminal-symbol , \ token ,
|
|
|
|
] [ ] make ;
|
|
|
|
|
2007-11-27 17:07:17 -05:00
|
|
|
M: ebnf-non-terminal ebnf-compile ( ast -- quot )
|
|
|
|
[
|
2007-11-27 17:25:34 -05:00
|
|
|
ebnf-non-terminal-symbol , \ in , \ get , \ lookup , \ execute ,
|
2007-11-27 17:07:17 -05:00
|
|
|
] [ ] make ;
|
|
|
|
|
2007-11-27 00:13:36 -05:00
|
|
|
M: ebnf-choice ebnf-compile ( ast -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
ebnf-choice-options [
|
|
|
|
ebnf-compile ,
|
|
|
|
] each
|
|
|
|
] { } make ,
|
2007-11-27 16:28:28 -05:00
|
|
|
[ call ] , \ map , \ choice ,
|
|
|
|
] [ ] make ;
|
|
|
|
|
|
|
|
M: ebnf-sequence ebnf-compile ( ast -- quot )
|
|
|
|
[
|
|
|
|
[
|
|
|
|
ebnf-sequence-elements [
|
|
|
|
ebnf-compile ,
|
|
|
|
] each
|
|
|
|
] { } make ,
|
|
|
|
[ call ] , \ map , \ seq ,
|
2007-11-27 00:13:36 -05:00
|
|
|
] [ ] make ;
|
|
|
|
|
2007-11-27 17:07:17 -05:00
|
|
|
M: ebnf-rule ebnf-compile ( ast -- quot )
|
|
|
|
[
|
|
|
|
dup ebnf-rule-symbol , \ in , \ get , \ create ,
|
|
|
|
ebnf-rule-elements ebnf-compile , \ define-compound ,
|
|
|
|
] [ ] make ;
|
|
|
|
|
2007-11-27 17:25:34 -05:00
|
|
|
M: ebnf ebnf-compile ( ast -- quot )
|
|
|
|
[
|
|
|
|
ebnf-rules [
|
|
|
|
ebnf-compile %
|
|
|
|
] each
|
|
|
|
] [ ] make ;
|
|
|
|
|
2007-11-27 00:13:36 -05:00
|
|
|
DEFER: 'rhs'
|
|
|
|
|
|
|
|
: 'non-terminal' ( -- parser )
|
|
|
|
CHAR: a CHAR: z range repeat1 [ >string <ebnf-non-terminal> ] action ;
|
|
|
|
|
|
|
|
: 'terminal' ( -- parser )
|
|
|
|
"\"" token hide [ CHAR: " = not ] satisfy repeat1 "\"" token hide 3array seq [ first >string <ebnf-terminal> ] action ;
|
|
|
|
|
|
|
|
: 'element' ( -- parser )
|
|
|
|
'non-terminal' 'terminal' 2array choice ;
|
|
|
|
|
|
|
|
: 'sequence' ( -- parser )
|
2007-11-27 16:28:28 -05:00
|
|
|
'element' sp
|
|
|
|
"|" token sp ensure-not 2array seq [ first ] action
|
|
|
|
repeat1 [ <ebnf-sequence> ] action ;
|
2007-11-27 00:13:36 -05:00
|
|
|
|
|
|
|
: 'choice' ( -- parser )
|
|
|
|
'element' sp "|" token sp list-of [ <ebnf-choice> ] action ;
|
|
|
|
|
|
|
|
: 'repeat0' ( -- parser )
|
|
|
|
"{" token sp hide
|
|
|
|
[ 'rhs' sp ] delay
|
|
|
|
"}" token sp hide
|
|
|
|
3array seq ;
|
|
|
|
|
|
|
|
: 'rhs' ( -- parser )
|
|
|
|
'repeat0'
|
|
|
|
'sequence'
|
2007-11-27 16:28:28 -05:00
|
|
|
'choice'
|
2007-11-27 00:13:36 -05:00
|
|
|
'element'
|
|
|
|
4array choice ;
|
|
|
|
|
|
|
|
: 'rule' ( -- parser )
|
2007-11-27 16:28:28 -05:00
|
|
|
'non-terminal' [ ebnf-non-terminal-symbol ] action
|
|
|
|
"=" token sp hide
|
2007-11-27 00:13:36 -05:00
|
|
|
'rhs'
|
2007-11-27 16:28:28 -05:00
|
|
|
3array seq [ first2 <ebnf-rule> ] action ;
|
2007-11-27 17:25:34 -05:00
|
|
|
|
|
|
|
: 'ebnf' ( -- parser )
|
|
|
|
'rule' sp ";" token sp hide list-of [ <ebnf> ] action ;
|
|
|
|
|
|
|
|
: ebnf>quot ( string -- quot )
|
|
|
|
'ebnf' parse [
|
|
|
|
parse-result-ast ebnf-compile
|
|
|
|
] [
|
|
|
|
f
|
|
|
|
] if* ;
|