Commit 35c767fc authored by Igor Dejanovic's avatar Igor Dejanovic

Fixing formatting in peg.py

parent d21f6388
...@@ -49,21 +49,23 @@ class PEGSemanticAction(SemanticAction): ...@@ -49,21 +49,23 @@ class PEGSemanticAction(SemanticAction):
def second_pass(self, parser, node): def second_pass(self, parser, node):
if isinstance(node, Terminal): if isinstance(node, Terminal):
return return
for i,n in enumerate(node.nodes): for i, n in enumerate(node.nodes):
if isinstance(n, Terminal): if isinstance(n, Terminal):
if n.value in parser.peg_rules: if n.value in parser.peg_rules:
node.nodes[i] = parser.peg_rules[n.value] node.nodes[i] = parser.peg_rules[n.value]
else: else:
raise SemanticError("Rule \"%s\" does not exists." % n) raise SemanticError("Rule \"%s\" does not exists." % n)
class SemGrammar(SemanticAction): class SemGrammar(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
return parser.peg_rules[parser.root_rule_name] return parser.peg_rules[parser.root_rule_name]
class SemRule(PEGSemanticAction): class SemRule(PEGSemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
rule_name = children[0].value rule_name = children[0].value
if len(children)>4: if len(children) > 4:
retval = Sequence(nodes=children[2:-1]) retval = Sequence(nodes=children[2:-1])
else: else:
retval = children[2] retval = children[2]
...@@ -77,26 +79,29 @@ class SemRule(PEGSemanticAction): ...@@ -77,26 +79,29 @@ class SemRule(PEGSemanticAction):
parser.peg_rules[rule_name] = retval parser.peg_rules[rule_name] = retval
return retval return retval
class SemSequence(PEGSemanticAction): class SemSequence(PEGSemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if len(children)>1: if len(children) > 1:
return Sequence(nodes=children) return Sequence(nodes=children)
else: else:
return children[0] return children[0]
class SemOrderedChoice(PEGSemanticAction): class SemOrderedChoice(PEGSemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if len(children)>1: if len(children) > 1:
retval = OrderedChoice(nodes=children[::2]) retval = OrderedChoice(nodes=children[::2])
else: else:
retval = children[0] retval = children[0]
return retval return retval
class SemPrefix(PEGSemanticAction): class SemPrefix(PEGSemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug: if parser.debug:
print("Prefix: {} ".format(str(children))) print("Prefix: {} ".format(str(children)))
if len(children)==2: if len(children) == 2:
if children[0] == NOT(): if children[0] == NOT():
retval = Not() retval = Not()
else: else:
...@@ -110,6 +115,7 @@ class SemPrefix(PEGSemanticAction): ...@@ -110,6 +115,7 @@ class SemPrefix(PEGSemanticAction):
return retval return retval
class SemSufix(PEGSemanticAction): class SemSufix(PEGSemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug: if parser.debug:
...@@ -132,27 +138,31 @@ class SemSufix(PEGSemanticAction): ...@@ -132,27 +138,31 @@ class SemSufix(PEGSemanticAction):
return retval return retval
class SemExpression(PEGSemanticAction): class SemExpression(PEGSemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug: if parser.debug:
print("Expression : {}".format(str(children))) print("Expression : {}".format(str(children)))
if len(children)==1: if len(children) == 1:
return children[0] return children[0]
else: else:
return children[1] return children[1]
class SemIdentifier(SemanticAction): class SemIdentifier(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug: if parser.debug:
print("Identifier {}.".format(node.value)) print("Identifier {}.".format(node.value))
return node return node
class SemRegEx(SemanticAction): class SemRegEx(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug: if parser.debug:
print("RegEx {}.".format(children[1].value)) print("RegEx {}.".format(children[1].value))
return RegExMatch(children[1].value) return RegExMatch(children[1].value)
class SemLiteral(SemanticAction): class SemLiteral(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
if parser.debug: if parser.debug:
...@@ -162,6 +172,7 @@ class SemLiteral(SemanticAction): ...@@ -162,6 +172,7 @@ class SemLiteral(SemanticAction):
match_str = match_str.replace("\\\\", "\\") match_str = match_str.replace("\\\\", "\\")
return StrMatch(match_str) return StrMatch(match_str)
class SemTerminal(SemanticAction): class SemTerminal(SemanticAction):
def first_pass(self, parser, node, children): def first_pass(self, parser, node, children):
return StrMatch(node.value) return StrMatch(node.value)
...@@ -182,7 +193,8 @@ for sem in [LEFT_ARROW, SLASH, STAR, QUESTION, PLUS, AND, NOT, OPEN, CLOSE]: ...@@ -182,7 +193,8 @@ for sem in [LEFT_ARROW, SLASH, STAR, QUESTION, PLUS, AND, NOT, OPEN, CLOSE]:
class ParserPEG(Parser): class ParserPEG(Parser):
def __init__(self, language_def, root_rule_name, comment_rule_name=None, *args, **kwargs): def __init__(self, language_def, root_rule_name, comment_rule_name=None,
*args, **kwargs):
super(ParserPEG, self).__init__(*args, **kwargs) super(ParserPEG, self).__init__(*args, **kwargs)
self.root_rule_name = root_rule_name self.root_rule_name = root_rule_name
...@@ -202,4 +214,3 @@ class ParserPEG(Parser): ...@@ -202,4 +214,3 @@ class ParserPEG(Parser):
parser.root_rule_name = self.root_rule_name parser.root_rule_name = self.root_rule_name
parser.parse(language_def) parser.parse(language_def)
return parser.getASG() return parser.getASG()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment