1 # Copyright (C) 2005-2008, Parrot Foundation.
6 TGE::Grammar - The base class for all tree grammars.
16 .namespace [ 'TGE'; 'Grammar' ]
21 newclass base, ['TGE';'Grammar']
22 addattribute base, 'rules' # the rules in the grammar (an array)
23 addattribute base, 'symbols' # used for tracking symbols parsed
24 # (often a hash, but grammar chooses
31 Create a new grammar object. [Not implemented: Optionally pass it a
32 grammar specification in a string.] The grammar object holds an array
33 of TGE::Rule objects, which are the semantics defined by the grammar.
37 .sub init :vtable :method
38 $P1 = new 'ResizablePMCArray'
39 setattribute self, 'rules', $P1
41 setattribute self, 'symbols', $P2
46 Add a rule to the current attribute grammar.
50 .sub 'add_rule' :method
56 # create a new attribute grammar rule
58 rule = new ['TGE';'Rule']
59 setattribute rule, 'type', type
60 setattribute rule, 'name', name
61 setattribute rule, 'parent', parent
62 setattribute rule, 'action', action
64 # add the new rule to the grammar object
65 $P3 = getattribute self, 'rules'
72 Use a precompiled grammar on a data structure. This returns an
73 object on which you can call methods to fetch attributes on the
74 I<top node> of the data structure.
82 newtree = new ['TGE';'Tree']
83 setattribute newtree, 'data', tree
84 setattribute newtree, 'grammar', self
85 visit = getattribute newtree, 'visit'
86 # Build up the visit hash
92 rules = getattribute self, 'rules'
97 if index < 0 goto end_loop
98 currule = rules[index]
99 typename = getattribute currule, 'type'
100 $P2 = visit[typename]
101 $I1 = does $P2, 'array'
102 if $I1 goto array_exists
103 $P2 = new 'ResizablePMCArray'
104 visit[typename] = $P2
110 newtree.'_scan_node'(tree, 'ROOT')
116 Add a symbol to the lookup table.
120 .sub 'add_symbol' :method
126 symbols = getattribute self, 'symbols'
128 $I0 = defined symbols[name]
130 symbols[name] = value
137 Return an iterator for the symbol lookup table.
141 .sub 'symbol_iter' :method
142 $P1 = getattribute self, 'symbols'
150 Produce a data dump of the current contents of the grammar object.
155 $P0 = getattribute self, 'rules'
158 print "VAR1 => { \n\t 'rules' =>\n"
175 # vim: expandtab shiftwidth=4 ft=pir: