98
|
1 |
"{ Package: 'stx:goodies/petitparser/devtools' }"
|
|
2 |
|
|
3 |
Object subclass:#XBGFParser
|
|
4 |
instanceVariableNames:'productions'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'PetitParser-Utils'
|
|
8 |
!
|
|
9 |
|
|
10 |
!XBGFParser class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
A parse to parse XBGF grammar definitions
|
|
15 |
|
|
16 |
[author:]
|
|
17 |
Jan Vrany <jan.vrany@fit.cvut.cz>
|
|
18 |
|
|
19 |
[instance variables:]
|
|
20 |
|
|
21 |
[class variables:]
|
|
22 |
|
|
23 |
[see also:]
|
|
24 |
http://slps.github.com/
|
|
25 |
|
|
26 |
"
|
|
27 |
! !
|
|
28 |
|
|
29 |
!XBGFParser methodsFor:'accessing'!
|
|
30 |
|
|
31 |
productionAt: id
|
|
32 |
productions isNil ifTrue:[productions := Dictionary new].
|
|
33 |
^productions at: id ifAbsentPut:[PPUnresolvedParser named: id]
|
|
34 |
|
|
35 |
"Created: / 10-01-2013 / 12:03:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
36 |
! !
|
|
37 |
|
|
38 |
!XBGFParser methodsFor:'parsing'!
|
|
39 |
|
|
40 |
parse: aStringOrStreamOrFilename
|
|
41 |
| doc |
|
|
42 |
|
|
43 |
doc := XML::XMLParser
|
|
44 |
processDocumentStream:aStringOrStreamOrFilename readStream
|
|
45 |
beforeScanDo:[:p|p validate: false].
|
|
46 |
self processDocument: doc.
|
|
47 |
^self
|
|
48 |
|
|
49 |
"Created: / 10-01-2013 / 12:00:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
50 |
! !
|
|
51 |
|
|
52 |
!XBGFParser methodsFor:'processing'!
|
|
53 |
|
|
54 |
processDocument: doc
|
|
55 |
( doc root / 'ns0:production' ) do:[:node|
|
|
56 |
self processProduction: node
|
|
57 |
]
|
|
58 |
|
|
59 |
"Created: / 10-01-2013 / 12:01:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
60 |
!
|
|
61 |
|
|
62 |
processExpression: node
|
|
63 |
|
|
64 |
| expr parser |
|
|
65 |
|
|
66 |
self assert: node children size == 1.
|
|
67 |
expr := node children anElement.
|
|
68 |
|
|
69 |
expr tag type = 'nonterminal' ifTrue:[
|
|
70 |
^self productionAt: expr characterData.
|
|
71 |
].
|
|
72 |
|
|
73 |
expr tag type = 'terminal' ifTrue:[
|
|
74 |
^PPLiteralSequenceParser on: expr characterData.
|
|
75 |
].
|
|
76 |
|
|
77 |
expr tag type = 'optional' ifTrue:[
|
|
78 |
^PPOptionalParser on: (self processExpression: (expr / 'ns0:expression') anElement)
|
|
79 |
].
|
|
80 |
|
|
81 |
expr tag type = 'any' ifTrue:[
|
|
82 |
^#any asParser
|
|
83 |
].
|
|
84 |
|
|
85 |
expr tag type = 'epsilon' ifTrue:[
|
|
86 |
^nil asParser
|
|
87 |
].
|
|
88 |
|
|
89 |
|
|
90 |
expr tag type = 'sequence' ifTrue:[
|
|
91 |
parser := PPSequenceParser new
|
|
92 |
] ifFalse:[expr tag type = 'choice' ifTrue:[
|
|
93 |
parser := PPChoiceParser new
|
|
94 |
] ifFalse:[expr tag type = 'star' ifTrue:[
|
|
95 |
parser := PPRepeatingParser new setMin: 0 max: SmallInteger maxVal.
|
|
96 |
parser setParser: (self processExpression: (expr / 'ns0:expression') anElement).
|
|
97 |
^parser
|
|
98 |
] ifFalse:[expr tag type = 'plus' ifTrue:[
|
|
99 |
parser := PPRepeatingParser new setMin: 1 max: SmallInteger maxVal.
|
|
100 |
parser setParser: (self processExpression: (expr / 'ns0:expression') anElement).
|
|
101 |
^parser
|
|
102 |
] ifFalse:[
|
|
103 |
self error: 'Unknown expression: ', expr tag type
|
|
104 |
]]]].
|
|
105 |
|
|
106 |
parser setParsers:
|
|
107 |
((expr / 'ns0:expression') collect:[:e|self processExpression: e]).
|
|
108 |
^parser
|
|
109 |
|
|
110 |
"Created: / 10-01-2013 / 12:05:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
111 |
!
|
|
112 |
|
|
113 |
processProduction: node
|
|
114 |
|
|
115 |
| name prod |
|
|
116 |
|
|
117 |
name := (node / 'nonterminal') characterData.
|
|
118 |
prod := self productionAt: name.
|
|
119 |
|
|
120 |
self assert: (node / 'ns0:expression') size == 1.
|
|
121 |
|
|
122 |
prod def: (self processExpression: (node / 'ns0:expression') anElement)
|
|
123 |
|
|
124 |
"Created: / 10-01-2013 / 12:01:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
|
125 |
! !
|
|
126 |
|
|
127 |
!XBGFParser class methodsFor:'documentation'!
|
|
128 |
|
|
129 |
version
|
|
130 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/devtools/XBGFParser.st,v 1.1 2013-01-10 12:29:16 vrany Exp $'
|
|
131 |
!
|
|
132 |
|
|
133 |
version_CVS
|
|
134 |
^ '$Header: /cvs/stx/stx/goodies/petitparser/devtools/XBGFParser.st,v 1.1 2013-01-10 12:29:16 vrany Exp $'
|
|
135 |
! !
|