Commit 59b4c15d authored by Igor Dejanovic's avatar Igor Dejanovic

Switch to standard python logging facility.

parent de1ffa74
......@@ -12,8 +12,10 @@
import re
import bisect
import logging
logger = logging.getLogger('arpeggio')
DEBUG = False
DEFAULT_WS='\t\n\r '
class ArpeggioError(Exception):
......@@ -47,10 +49,6 @@ class NoMatch(Exception):
self._up = True # By default when NoMatch is thrown we will go up the Parse Model Tree.
def _log(msg):
if DEBUG:
print msg
def flatten(_iterable):
'''Flattening of python iterables.'''
result = []
......@@ -105,7 +103,7 @@ class ParsingExpression(object):
return id(self)
def _parse_intro(self, parser):
_log("Parsing %s" % self.name)
logger.debug("Parsing %s" % self.name)
results = []
parser._skip_ws()
self.c_pos = parser.position
......@@ -117,7 +115,7 @@ class ParsingExpression(object):
#If this position is already parsed by this parser expression than use
#the result
if self.result_cache.has_key(self.c_pos):
_log("Result for [%s, %s] founded in result_cache." % (self, self.c_pos))
logger.debug("Result for [%s, %s] founded in result_cache." % (self, self.c_pos))
result, new_pos = self.result_cache[self.c_pos]
parser.position = new_pos
return result
......@@ -362,10 +360,10 @@ class RegExMatch(Match):
m = self.regex.match(parser.input[parser.position:])
if m:
parser.position += len(m.group())
_log("Match %s at %d" % (m.group(), self.c_pos))
logger.debug("Match %s at %d" % (m.group(), self.c_pos))
return Terminal(self.rule if self.root else '', self.c_pos, m.group())
else:
_log("NoMatch at %d" % self.c_pos)
logger.debug("NoMatch at %d" % self.c_pos)
parser._nm_raise(self.root if self.root else self.name, self.c_pos, parser)
class StrMatch(Match):
......@@ -382,10 +380,10 @@ class StrMatch(Match):
def _parse(self, parser):
if parser.input[parser.position:].startswith(self.to_match):
parser.position += len(self.to_match)
_log("Match %s at %d" % (self.to_match, self.c_pos))
logger.debug("Match %s at %d" % (self.to_match, self.c_pos))
return Terminal(self.rule if self.root else '', self.c_pos, self.to_match)
else:
_log("NoMatch at %d" % self.c_pos)
logger.debug("NoMatch at %d" % self.c_pos)
parser._nm_raise(self.to_match, self.c_pos, parser)
def __str__(self):
......@@ -422,7 +420,7 @@ class EndOfFile(Match):
if len(parser.input) == parser.position:
return Terminal(self.rule if self.root else '', self.c_pos, 'EOF')
else:
_log("EOF not matched.")
logger.debug("EOF not matched.")
parser._nm_raise(self.name, self.c_pos, parser)
......@@ -579,10 +577,10 @@ class Parser(object):
return retval
_log("ASG: First pass")
logger.debug("ASG: First pass")
asg = tree_walk(self.parse_tree)
_log("ASG: Second pass")
logger.debug("ASG: Second pass")
# Second pass
for sa_name, asg_node in for_second_pass:
sem_actions[sa_name].second_pass(self, asg_node)
......@@ -679,7 +677,7 @@ class ParserPython(Parser):
root = False
while callable(expression): # Is this expression a parser rule?
if self.__rule_cache.has_key(expression.__name__):
_log("Rule %s founded in cache." % expression.__name__)
logger.debug("Rule %s founded in cache." % expression.__name__)
return self.__rule_cache.get(expression.__name__)
rule = expression.__name__
root = True
......@@ -687,7 +685,7 @@ class ParserPython(Parser):
if hasattr(expression, "sem"):
self.sem_actions[rule] = expression.sem
_log("push : %s" % rule)
logger.debug("push : %s" % rule)
self.__rule_stack.append(rule)
expression = expression()
......@@ -715,7 +713,7 @@ class ParserPython(Parser):
# in order to support recursive definitions
if root:
self.__rule_cache[rule] = retval
_log("New rule: %s -> %s" % (rule, retval.__class__.__name__))
logger.debug("New rule: %s -> %s" % (rule, retval.__class__.__name__))
retval.nodes = [self._from_python(e) for e in expression]
elif type(expression) is str:
......@@ -730,7 +728,7 @@ class ParserPython(Parser):
if root:
name = self.__rule_stack.pop()
_log("pop: %s" % name)
logger.debug("pop: %s" % name)
return retval
......
......@@ -10,8 +10,10 @@
__all__ = ['ParserPEG']
from arpeggio import *
from arpeggio import _log
from arpeggio import RegExMatch as _
import logging
logger = logging.getLogger('arpeggio.peg')
# PEG Grammar
def grammar(): return OneOrMore(rule), EOF
......@@ -93,7 +95,7 @@ class SemOrderedChoice(PEGSemanticAction):
class SemPrefix(PEGSemanticAction):
def first_pass(self, parser, node, nodes):
_log("Prefix: %s " % str(nodes))
logger.debug("Prefix: %s " % str(nodes))
if len(nodes)==2:
if nodes[0] == NOT():
retval = Not()
......@@ -110,9 +112,9 @@ class SemPrefix(PEGSemanticAction):
class SemSufix(PEGSemanticAction):
def first_pass(self, parser, node, nodes):
_log("Sufix : %s" % str(nodes))
logger.debug("Sufix : %s" % str(nodes))
if len(nodes) == 2:
_log("Sufix : %s" % str(nodes[1]))
logger.debug("Sufix : %s" % str(nodes[1]))
if nodes[1] == STAR():
retval = ZeroOrMore(nodes[0])
elif nodes[1] == QUESTION():
......@@ -130,7 +132,7 @@ class SemSufix(PEGSemanticAction):
class SemExpression(PEGSemanticAction):
def first_pass(self, parser, node, nodes):
_log("Expression : %s" % str(nodes))
logger.debug("Expression : %s" % str(nodes))
if len(nodes)==1:
return nodes[0]
else:
......@@ -138,17 +140,17 @@ class SemExpression(PEGSemanticAction):
class SemIdentifier(SemanticAction):
def first_pass(self, parser, node, nodes):
_log("Identifier %s." % node.value)
logger.debug("Identifier %s." % node.value)
return node
class SemRegEx(SemanticAction):
def first_pass(self, parser, node, nodes):
_log("RegEx %s." % nodes[2].value)
logger.debug("RegEx %s." % nodes[2].value)
return RegExMatch(nodes[2].value)
class SemLiteral(SemanticAction):
def first_pass(self, parser, node, nodes):
_log("Literal: %s" % node.value)
logger.debug("Literal: %s" % node.value)
match_str = node.value[1:-1]
match_str = match_str.replace("\\'", "'")
match_str = match_str.replace("\\\\", "\\")
......@@ -197,6 +199,8 @@ class ParserPEG(Parser):
if __name__ == "__main__":
try:
logging.basicConfig(level=logging.DEBUG)
parser = ParserPython(grammar, None)
f = open("peg_parser_model.dot", "w")
......
......@@ -13,7 +13,7 @@
from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio import RegExMatch as _
from arpeggio import _log
import logging
def number(): return _(r'\d*\.\d*|\d+')
def factor(): return [number, ("(", expression, ")")]
......@@ -25,13 +25,13 @@ def calc(): return expression, EndOfFile
class ToFloat(SemanticAction):
'''Converts node value to float.'''
def first_pass(self, parser, node, nodes):
_log("Converting %s." % node.value)
logging.debug("Converting %s." % node.value)
return float(node.value)
class Factor(SemanticAction):
'''Removes parenthesis if exists and returns what was contained inside.'''
def first_pass(self, parser, node, nodes):
_log("Factor %s" % nodes)
logging.debug("Factor %s" % nodes)
if nodes[0] == "(":
return nodes[1]
else:
......@@ -43,14 +43,14 @@ class Term(SemanticAction):
Factor nodes will be already evaluated.
'''
def first_pass(self, parser, node, nodes):
_log("Term %s" % nodes)
logging.debug("Term %s" % nodes)
term = nodes[0]
for i in range(2, len(nodes), 2):
if nodes[i-1]=="*":
term *= nodes[i]
else:
term /= nodes[i]
_log("Term = %f" % term)
logging.debug("Term = %f" % term)
return term
class Expr(SemanticAction):
......@@ -59,7 +59,7 @@ class Expr(SemanticAction):
Term nodes will be already evaluated.
'''
def first_pass(self, parser, node, nodes):
_log("Expression %s" % nodes)
logging.debug("Expression %s" % nodes)
expr = 0
start = 0
# Check for unary + or - operator
......@@ -72,7 +72,7 @@ class Expr(SemanticAction):
else:
expr += nodes[i]
_log("Expression = %f" % expr)
logging.debug("Expression = %f" % expr)
return expr
class Calc(SemanticAction):
......@@ -88,10 +88,7 @@ calc.sem = Calc()
if __name__ == "__main__":
try:
import arpeggio
# Setting DEBUG to true will show log messages.
arpeggio.DEBUG = True
logging.basicConfig(level=logging.DEBUG)
# First we will make a parser - an instance of the calc parser model.
# Parser model is given in the form of python constructs therefore we
......
......@@ -16,6 +16,7 @@
from arpeggio import *
from arpeggio.peg import ParserPEG
from arpeggio.export import PMDOTExport, PTDOTExport
import logging
# Semantic actions
from calc import ToFloat, Factor, Term, Expr, Calc
......@@ -39,9 +40,7 @@ sem_actions = {
}
try:
# Turning debugging on
import arpeggio
arpeggio.DEBUG = True
logging.basicConfig(level=logging.DEBUG)
# First we will make a parser - an instance of the calc parser model.
# Parser model is given in the form of PEG notation therefore we
......
......@@ -36,6 +36,7 @@ value
from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio import RegExMatch as _
import logging
def TRUE(): return "true"
def FALSE(): return "false"
......@@ -82,9 +83,7 @@ if __name__ == "__main__":
}
"""
try:
import arpeggio
arpeggio.DEBUG = True
logging.basicConfig(level=logging.DEBUG)
# Creating parser from parser model.
parser = ParserPython(jsonFile)
......
......@@ -12,9 +12,9 @@
##############################################################################
from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio import _log
from arpeggio import RegExMatch as _
from arpeggio.peg import ParserPEG
import logging
# Semantic actions
from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence, SemPrefix, \
......@@ -66,8 +66,7 @@ peg_grammar = r"""
try:
import arpeggio
arpeggio.DEBUG = True
logging.basicConfig(level=logging.DEBUG)
# ParserPEG will use ParserPython to parse peg_grammar definition and
# create parser_model for parsing PEG based grammars
......
......@@ -12,6 +12,7 @@
from arpeggio import *
from arpeggio.export import PMDOTExport, PTDOTExport
from arpeggio import RegExMatch as _
import logging
def comment(): return [_("//.*"), _("/\*.*\*/")]
def literal(): return _(r'\d*\.\d*|\d+|".*?"')
......@@ -30,8 +31,7 @@ def function(): return Kwd("function"), symbol, parameterlist, block
def simpleLanguage(): return function
try:
import arpeggio
arpeggio.DEBUG = True
logging.basicConfig(level=logging.DEBUG)
# Parser instantiation. simpleLanguage is root definition and comment is
# grammar rule for comments.
......
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