self,
pyacc,
section,
+ character_to_symbol,
name_to_symbol
):
raise NotImplementedException
class PYACC(element.Element):
# internal classes
class Symbol(element.Element):
- # GENERATE ELEMENT(str name, list(int) character_set) BEGIN
+ # GENERATE ELEMENT(str name, list(int) character_set, int precedence, int associativity) BEGIN
def __init__(
self,
tag = 'PYACC_Symbol',
text = '',
children = [],
name = '',
- character_set = []
+ character_set = [],
+ precedence = -1,
+ associativity = -1
):
element.Element.__init__(
self,
if isinstance(character_set, str) else
character_set
)
+ self.precedence = (
+ element.deserialize_int(precedence)
+ if isinstance(precedence, str) else
+ precedence
+ )
+ self.associativity = (
+ element.deserialize_int(associativity)
+ if isinstance(associativity, str) else
+ associativity
+ )
def serialize(self, ref_list, indent = 0):
element.Element.serialize(self, ref_list, indent)
self.set('name', element.serialize_str(self.name))
'character_set',
' '.join([element.serialize_int(i) for i in self.character_set])
)
+ self.set('precedence', element.serialize_int(self.precedence))
+ self.set('associativity', element.serialize_int(self.associativity))
def deserialize(self, ref_list):
element.Element.deserialize(self, ref_list)
self.name = element.deserialize_str(self.get('name', ''))
element.deserialize_int(i)
for i in self.get('character_set', '').split()
]
+ self.precedence = element.deserialize_int(self.get('precedence', '-1'))
+ self.associativity = element.deserialize_int(self.get('associativity', '-1'))
def copy(self, factory = None):
result = element.Element.copy(
self,
)
result.name = self.name
result.character_set = self.character_set
+ result.precedence = self.precedence
+ result.associativity = self.associativity
return result
def repr_serialize(self, params):
element.Element.repr_serialize(self, params)
', '.join([repr(i) for i in self.character_set])
)
)
+ if self.precedence != -1:
+ params.append(
+ 'precedence = {0:s}'.format(repr(self.precedence))
+ )
+ if self.associativity != -1:
+ params.append(
+ 'associativity = {0:s}'.format(repr(self.associativity))
+ )
def __repr__(self):
params = []
self.repr_serialize(params)
def post_process(
self,
pyacc,
+ character_to_symbol,
name_to_symbol
):
raise NotImplementedException
]
)
- class Tag(element.Element):
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_Tag',
- attrib = {},
- text = '',
- children = []
- ):
- element.Element.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = element.Element.copy(
- self,
- Tag if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.Tag({0:s})'.format(', '.join(params))
- # GENERATE END
-
- class TagNone(element.Element):
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_TagNone',
- attrib = {},
- text = '',
- children = []
- ):
- element.Element.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = element.Element.copy(
- self,
- TagNone if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.TagNone({0:s})'.format(', '.join(params))
- # GENERATE END
-
class Text(element.Element):
# GENERATE ELEMENT() BEGIN
def __init__(
# GENERATE END
class Section1Or2(Section):
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_Section1Or2',
- attrib = {},
- text = '',
- children = []
- ):
- PYACC.Section.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = PYACC.Section.copy(
- self,
- Section1Or2 if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.Section1Or2({0:s})'.format(', '.join(params))
- # GENERATE END
- def post_process(
- self,
- pyacc,
- name_to_symbol
- ):
- for i in self:
- i.post_process(
- pyacc,
- self,
- name_to_symbol
- )
-
class Code(Item):
# GENERATE ELEMENT() BEGIN
def __init__(
# GENERATE END
class CodeProps(Item):
- class GenericSymList(element.Element):
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_Section1Or2_CodeProps_GenericSymList',
- attrib = {},
- text = '',
- children = []
- ):
- element.Element.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = element.Element.copy(
- self,
- GenericSymList if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.Section1Or2.CodeProps.GenericSymList({0:s})'.format(', '.join(params))
- # GENERATE END
-
# GENERATE ELEMENT() BEGIN
def __init__(
self,
# GENERATE END
class Precedence(Item):
- class Symbols(element.Element):
- class Symbol(element.Element):
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_Section1Or2_Precedence_Symbols_Symbol',
- attrib = {},
- text = '',
- children = []
- ):
- element.Element.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = element.Element.copy(
- self,
- Symbol if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.Section1Or2.Precedence.Symbols.Symbol({0:s})'.format(', '.join(params))
- # GENERATE END
-
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_Section1Or2_Precedence_Symbols',
- attrib = {},
- text = '',
- children = []
- ):
- element.Element.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = element.Element.copy(
- self,
- Symbols if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.Section1Or2.Precedence.Symbols({0:s})'.format(', '.join(params))
- # GENERATE END
-
- # GENERATE ELEMENT() BEGIN
+ # GENERATE ELEMENT(int type) BEGIN
def __init__(
self,
tag = 'PYACC_Section1Or2_Precedence',
attrib = {},
text = '',
- children = []
+ children = [],
+ type = -1
):
Item.__init__(
self,
text,
children
)
+ self.type = (
+ element.deserialize_int(type)
+ if isinstance(type, str) else
+ type
+ )
+ def serialize(self, ref_list, indent = 0):
+ Item.serialize(self, ref_list, indent)
+ self.set('type', element.serialize_int(self.type))
+ def deserialize(self, ref_list):
+ Item.deserialize(self, ref_list)
+ self.type = element.deserialize_int(self.get('type', '-1'))
def copy(self, factory = None):
result = Item.copy(
self,
Precedence if factory is None else factory
)
+ result.type = self.type
return result
+ def repr_serialize(self, params):
+ Item.repr_serialize(self, params)
+ if self.type != -1:
+ params.append(
+ 'type = {0:s}'.format(repr(self.type))
+ )
def __repr__(self):
params = []
self.repr_serialize(params)
return 'ast.PYACC.Section1Or2.Precedence({0:s})'.format(', '.join(params))
# GENERATE END
+ def post_process(
+ self,
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol
+ ):
+ self[0].post_process(
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ pyacc.precedences, # precedence
+ self.type # associativity
+ )
+ pyacc.precedences += 1
class Start(Item):
# GENERATE ELEMENT() BEGIN
return 'ast.PYACC.Section1Or2.Start({0:s})'.format(', '.join(params))
# GENERATE END
- class SymbolDef(Item):
+ class TagOrSymbol(element.Element):
# GENERATE ELEMENT() BEGIN
def __init__(
self,
- tag = 'PYACC_Section1Or2_SymbolDef',
+ tag = 'PYACC_Section1Or2_TagOrSymbol',
attrib = {},
text = '',
children = []
):
- Item.__init__(
+ element.Element.__init__(
self,
tag,
attrib,
children
)
def copy(self, factory = None):
- result = Item.copy(
+ result = element.Element.copy(
+ self,
+ TagOrSymbol if factory is None else factory
+ )
+ return result
+ def __repr__(self):
+ params = []
+ self.repr_serialize(params)
+ return 'ast.PYACC.Section1Or2.TagOrSymbol({0:s})'.format(', '.join(params))
+ # GENERATE END
+ def post_process(
+ self,
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ precedence,
+ associativity,
+ tag
+ ):
+ raise NotImplementedException
+
+ class Tag(TagOrSymbol):
+ # GENERATE ELEMENT(int type) BEGIN
+ def __init__(
+ self,
+ tag = 'PYACC_Section1Or2_Tag',
+ attrib = {},
+ text = '',
+ children = [],
+ type = -1
+ ):
+ PYACC.Section1Or2.TagOrSymbol.__init__(
+ self,
+ tag,
+ attrib,
+ text,
+ children
+ )
+ self.type = (
+ element.deserialize_int(type)
+ if isinstance(type, str) else
+ type
+ )
+ def serialize(self, ref_list, indent = 0):
+ PYACC.Section1Or2.TagOrSymbol.serialize(self, ref_list, indent)
+ self.set('type', element.serialize_int(self.type))
+ def deserialize(self, ref_list):
+ PYACC.Section1Or2.TagOrSymbol.deserialize(self, ref_list)
+ self.type = element.deserialize_int(self.get('type', '-1'))
+ def copy(self, factory = None):
+ result = PYACC.Section1Or2.TagOrSymbol.copy(
+ self,
+ Tag if factory is None else factory
+ )
+ result.type = self.type
+ return result
+ def repr_serialize(self, params):
+ PYACC.Section1Or2.TagOrSymbol.repr_serialize(self, params)
+ if self.type != -1:
+ params.append(
+ 'type = {0:s}'.format(repr(self.type))
+ )
+ def __repr__(self):
+ params = []
+ self.repr_serialize(params)
+ return 'ast.PYACC.Section1Or2.Tag({0:s})'.format(', '.join(params))
+ # GENERATE END
+ def post_process(
+ self,
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ precedence,
+ associativity,
+ tag
+ ):
+ return self
+
+ class Symbol(TagOrSymbol):
+ # GENERATE ELEMENT() BEGIN
+ def __init__(
+ self,
+ tag = 'PYACC_Section1Or2_Symbol',
+ attrib = {},
+ text = '',
+ children = []
+ ):
+ PYACC.Section1Or2.TagOrSymbol.__init__(
+ self,
+ tag,
+ attrib,
+ text,
+ children
+ )
+ def copy(self, factory = None):
+ result = PYACC.Section1Or2.TagOrSymbol.copy(
+ self,
+ Symbol if factory is None else factory
+ )
+ return result
+ def __repr__(self):
+ params = []
+ self.repr_serialize(params)
+ return 'ast.PYACC.Section1Or2.Symbol({0:s})'.format(', '.join(params))
+ # GENERATE END
+ def post_process(
+ self,
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ precedence,
+ associativity,
+ tag
+ ):
+ if isinstance(self[0], PYACC.Char):
+ character = ord(self[0].get_text())
+ assert character != 0 # would conflict with YYEOF
+ if character in character_to_symbol:
+ symbol = character_to_symbol[character]
+ assert symbol >= 0
+ else:
+ symbol = len(pyacc.terminal_symbols)
+ character_to_symbol[character] = symbol
+ pyacc.terminal_symbols.append(
+ PYACC.Symbol(
+ character_set = [character, character + 1]
+ )
+ )
+ elif isinstance(self[0], PYACC.ID):
+ name = element.get_text(self[0], 0)
+ if name in name_to_symbol:
+ symbol = name_to_symbol[name]
+ assert symbol >= 0
+ else:
+ symbol = len(pyacc.terminal_symbols)
+ name_to_symbol[name] = symbol
+ character = 0x100 + symbol # fix this later
+ pyacc.terminal_symbols.append(
+ PYACC.Symbol(
+ name = name,
+ character_set = [character, character + 1]
+ )
+ )
+ else:
+ assert False
+ if precedence >= 0:
+ assert pyacc.terminal_symbols[symbol].precedence == -1
+ pyacc.terminal_symbols[symbol].precedence = precedence
+ if associativity >= 0:
+ assert pyacc.terminal_symbols[symbol].associativity == -1
+ pyacc.terminal_symbols[symbol].associativity = associativity
+ return tag
+
+ class TaggedSymbols(element.Element):
+ # GENERATE ELEMENT() BEGIN
+ def __init__(
+ self,
+ tag = 'PYACC_Section1Or2_TaggedSymbols',
+ attrib = {},
+ text = '',
+ children = []
+ ):
+ element.Element.__init__(
+ self,
+ tag,
+ attrib,
+ text,
+ children
+ )
+ def copy(self, factory = None):
+ result = element.Element.copy(
self,
- SymbolDef if factory is None else factory
+ TaggedSymbols if factory is None else factory
)
return result
def __repr__(self):
params = []
self.repr_serialize(params)
- return 'ast.PYACC.Section1Or2.SymbolDef({0:s})'.format(', '.join(params))
+ return 'ast.PYACC.Section1Or2.TaggedSymbols({0:s})'.format(', '.join(params))
# GENERATE END
+ def post_process(
+ self,
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ precedence,
+ associativity
+ ):
+ tag = None
+ for i in self:
+ tag = i.post_process(
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ precedence,
+ associativity,
+ tag
+ )
class Token(Item):
# GENERATE ELEMENT() BEGIN
self,
pyacc,
section,
+ character_to_symbol,
name_to_symbol
):
- for i in self:
- assert isinstance(i, PYACC.ID)
- token_name = element.get_text(i, 0)
- assert token_name not in name_to_symbol
- name_to_symbol[token_name] = len(pyacc.terminal_symbols)
- character = 0x100 + len(pyacc.terminal_symbols)
- pyacc.terminal_symbols.append(
- PYACC.Symbol(
- name = token_name,
- character_set = [character, character + 1]
- )
- )
-
- class Type(Item):
- class Symbols(element.Element):
- # GENERATE ELEMENT() BEGIN
- def __init__(
- self,
- tag = 'PYACC_Section1Or2_Type_Symbols',
- attrib = {},
- text = '',
- children = []
- ):
- element.Element.__init__(
- self,
- tag,
- attrib,
- text,
- children
- )
- def copy(self, factory = None):
- result = element.Element.copy(
- self,
- Symbols if factory is None else factory
- )
- return result
- def __repr__(self):
- params = []
- self.repr_serialize(params)
- return 'ast.PYACC.Section1Or2.Type.Symbols({0:s})'.format(', '.join(params))
- # GENERATE END
+ self[0].post_process(
+ pyacc,
+ section,
+ character_to_symbol,
+ name_to_symbol,
+ -1, # precedence
+ -1 # associativity
+ )
+ class Type(Item):
# GENERATE ELEMENT() BEGIN
def __init__(
self,
return 'ast.PYACC.Section1Or2.Union({0:s})'.format(', '.join(params))
# GENERATE END
+ # GENERATE ELEMENT() BEGIN
+ def __init__(
+ self,
+ tag = 'PYACC_Section1Or2',
+ attrib = {},
+ text = '',
+ children = []
+ ):
+ PYACC.Section.__init__(
+ self,
+ tag,
+ attrib,
+ text,
+ children
+ )
+ def copy(self, factory = None):
+ result = PYACC.Section.copy(
+ self,
+ Section1Or2 if factory is None else factory
+ )
+ return result
+ def __repr__(self):
+ params = []
+ self.repr_serialize(params)
+ return 'ast.PYACC.Section1Or2({0:s})'.format(', '.join(params))
+ # GENERATE END
+ def post_process(
+ self,
+ pyacc,
+ character_to_symbol,
+ name_to_symbol
+ ):
+ for i in self:
+ i.post_process(
+ pyacc,
+ self,
+ character_to_symbol,
+ name_to_symbol
+ )
+
class Section1(Section1Or2):
class Define(Item):
# GENERATE ELEMENT() BEGIN
self,
pyacc,
section,
+ character_to_symbol,
name_to_symbol
):
assert isinstance(self[0], PYACC.Text)
self,
pyacc,
lhs_symbol,
+ character_to_symbol,
name_to_symbol
):
production = grammar.Grammar.Production(
if isinstance(self[i][0], PYACC.Char):
character = ord(self[i][0].get_text())
assert character != 0 # would conflict with YYEOF
- pyacc.characters_used.add(character)
+ if character not in character_to_symbol:
+ character_to_symbol[character] = len(pyacc.terminal_symbols)
+ pyacc.terminal_symbols.append(
+ PYACC.Symbol(
+ character_set = [character, character + 1]
+ )
+ )
+ pyacc.characters_used.add(character) # remove this later
production.append(
grammar.Grammar.Production.Symbol(
terminal_set = [character, character + 1]
self,
pyacc,
section,
+ character_to_symbol,
name_to_symbol
):
assert isinstance(self[0], PYACC.ID)
lhs_symbol = ~i
else:
lhs_symbol = len(pyacc.nonterminal_symbols)
+ character_to_symbol,
name_to_symbol[lhs_name] = ~lhs_symbol
pyacc.nonterminal_symbols.append(
PYACC.Symbol(name = lhs_name, character_set = [])
i.post_process(
pyacc,
lhs_symbol,
+ character_to_symbol,
name_to_symbol
)
def post_process(
self,
pyacc,
+ character_to_symbol,
name_to_symbol
):
pass
# GENERATE END
- # GENERATE ELEMENT(list(ref) prologue_text, set(int) characters_used, list(ref) terminal_symbols, list(ref) nonterminal_symbols, ref grammar, list(ref) actions_braced_code) BEGIN
+ # GENERATE ELEMENT(list(ref) prologue_text, set(int) characters_used, int precedences, list(ref) terminal_symbols, list(ref) nonterminal_symbols, ref grammar, list(ref) actions_braced_code) BEGIN
def __init__(
self,
tag = 'PYACC',
children = [],
prologue_text = [],
characters_used = set(),
+ precedences = -1,
terminal_symbols = [],
nonterminal_symbols = [],
grammar = None,
if isinstance(characters_used, str) else
characters_used
)
+ self.precedences = (
+ element.deserialize_int(precedences)
+ if isinstance(precedences, str) else
+ precedences
+ )
self.terminal_symbols = terminal_symbols
self.nonterminal_symbols = nonterminal_symbols
self.grammar = grammar
'characters_used',
' '.join([element.serialize_int(i) for i in sorted(self.characters_used)])
)
+ self.set('precedences', element.serialize_int(self.precedences))
self.set(
'terminal_symbols',
' '.join([element.serialize_ref(i, ref_list) for i in self.terminal_symbols])
for i in self.get('characters_used', '').split()
]
)
+ self.precedences = element.deserialize_int(self.get('precedences', '-1'))
self.terminal_symbols = [
element.deserialize_ref(i, ref_list)
for i in self.get('terminal_symbols', '').split()
)
result.prologue_text = self.prologue_text
result.characters_used = self.characters_used
+ result.precedences = self.precedences
result.terminal_symbols = self.terminal_symbols
result.nonterminal_symbols = self.nonterminal_symbols
result.grammar = self.grammar
', '.join([repr(i) for i in sorted(self.characters_used)])
)
)
+ if self.precedences != -1:
+ params.append(
+ 'precedences = {0:s}'.format(repr(self.precedences))
+ )
if len(self.terminal_symbols):
params.append(
'terminal_symbols = [{0:s}]'.format(
# variables that will be serialized
self.prologue_text = []
self.characters_used = set()
+ self.precedences = 0
self.terminal_symbols = [
PYACC.Symbol(name = 'error', character_set = [0x100, 0x101]),
PYACC.Symbol(name = '$undefined', character_set = [0x101, 0x102])
self.actions_braced_code = []
# variables that won't be serialized
+ character_to_symbol = {} # indexed by int, always >= 0 (terminal)
# note: in name_to_symbol, >= 0 is terminal, < 0 is ~nonterminal
# (don't bother storing the '$undefined', it can't be looked up)
name_to_symbol = {'error': 0}
for i in self:
i.post_process(
self,
+ character_to_symbol,
name_to_symbol
)
'PYACC_StackLocation': PYACC.StackLocation,
'PYACC_StackReference': PYACC.StackReference,
'PYACC_String': PYACC.String,
- 'PYACC_Tag': PYACC.Tag,
- 'PYACC_TagNone': PYACC.TagNone,
'PYACC_Text': PYACC.Text,
'PYACC_Section1Or2': PYACC.Section1Or2,
'PYACC_Section1Or2_Code': PYACC.Section1Or2.Code,
'PYACC_Section1Or2_CodeProps': PYACC.Section1Or2.CodeProps,
- 'PYACC_Section1Or2_CodeProps_GenericSymList': PYACC.Section1Or2.CodeProps.GenericSymList,
'PYACC_Section1Or2_DefaultPrec': PYACC.Section1Or2.DefaultPrec,
'PYACC_Section1Or2_NTerm': PYACC.Section1Or2.NTerm,
'PYACC_Section1Or2_Precedence': PYACC.Section1Or2.Precedence,
- 'PYACC_Section1Or2_Precedence_Symbols': PYACC.Section1Or2.Precedence.Symbols,
- 'PYACC_Section1Or2_Precedence_Symbols_Symbol': PYACC.Section1Or2.Precedence.Symbols.Symbol,
'PYACC_Section1Or2_Start': PYACC.Section1Or2.Start,
- 'PYACC_Section1Or2_SymbolDef': PYACC.Section1Or2.SymbolDef,
+ 'PYACC_Section1Or2_TagOrSymbol': PYACC.Section1Or2.TagOrSymbol,
+ 'PYACC_Section1Or2_Tag': PYACC.Section1Or2.Tag,
+ 'PYACC_Section1Or2_Symbol': PYACC.Section1Or2.Symbol,
+ 'PYACC_Section1Or2_TaggedSymbols': PYACC.Section1Or2.TaggedSymbols,
'PYACC_Section1Or2_Token': PYACC.Section1Or2.Token,
'PYACC_Section1Or2_Type': PYACC.Section1Or2.Type,
- 'PYACC_Section1Or2_Type_Symbols': PYACC.Section1Or2.Type.Symbols,
'PYACC_Section1Or2_Union': PYACC.Section1Or2.Union,
'PYACC_Section1': PYACC.Section1,
'PYACC_Section1_Define': PYACC.Section1.Define,