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

Switch to standard python logging facility.

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