aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--imago/sgfParser/astNode.py21
-rw-r--r--imago/sgfParser/sgf.py3
-rwxr-xr-ximago/sgfParser/sgflex.py24
-rwxr-xr-ximago/sgfParser/sgfyacc.py13
-rwxr-xr-xsgfyacc.py67
5 files changed, 27 insertions, 101 deletions
diff --git a/imago/sgfParser/astNode.py b/imago/sgfParser/astNode.py
index e92223b..e2b74a2 100644
--- a/imago/sgfParser/astNode.py
+++ b/imago/sgfParser/astNode.py
@@ -97,21 +97,21 @@ class ASTNode:
newMove.previousMove = gameMove
return gameMove
-def textToCoords(text): # Poner en PropertyMove, subclase de Property
- col = ord(text[1]) - ord('a')
- row = ord(text[0]) - ord('a')
- return [row, col]
-
-
- def toString(self):
+ def toString(self, debug=False):
"""Returns a depth-first representation of the tree."""
out = '(' + str(self.props) + ')'
- out += "in"
+ if debug: out += "in"
for node in self.children:
out = out + node.toString()
- out += "out"
+ if debug: out += "out"
return out
+def textToCoords(text): # Poner en PropertyMove, subclase de Property
+ col = ord(text[1]) - ord('a')
+ row = ord(text[0]) - ord('a')
+ return [row, col]
+
+
class Property:
"""Property of a Node"""
def __init__(self, name, value):
@@ -124,3 +124,6 @@ class Property:
self.value.append(value)
else:
self.value = [self.value, value]
+
+ def toString(self):
+ return("Property(%s, %s)" % (self.name, self.value))
diff --git a/imago/sgfParser/sgf.py b/imago/sgfParser/sgf.py
index 3263650..56135de 100644
--- a/imago/sgfParser/sgf.py
+++ b/imago/sgfParser/sgf.py
@@ -1,10 +1,11 @@
"""Module for reading and writing of SGF files."""
from imago.gameLogic.gameMove import GameMove
+from imago.sgfParser.sgfyacc import parser
def loadGameTree(filename):
# PLY?
"""Parses a GameTree instance from a source SGF file."""
sgf = open(filename, "r")
- a = GameTree()
+ return parser.parse(sgf).toGameMoveTree()
diff --git a/imago/sgfParser/sgflex.py b/imago/sgfParser/sgflex.py
index e102447..c7dbf38 100755
--- a/imago/sgfParser/sgflex.py
+++ b/imago/sgfParser/sgflex.py
@@ -1,4 +1,4 @@
-#!/bin/python
+#!/usr/bin/python
# --------------------------------------
# sgflex.py
@@ -12,8 +12,6 @@ tokens = (
'LPAREN',
'RPAREN',
'SCOLON',
- 'LSQBRACKET',
- 'RSQBRACKET',
'PROPID',
'UCLETTER',
'DIGIT',
@@ -29,8 +27,6 @@ tokens = (
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_SCOLON = r';'
-t_LSQBRACKET = r'\['
-t_RSQBRACKET = r'\]'
t_PROPID = r'[A-Z][A-Z]?'
t_UCLETTER = r'[A-Z]'
t_DIGIT = r'[0-9]'
@@ -53,14 +49,12 @@ def t_DOUBLE(t):
def t_PROPVALUE(t):
r'\[[^\]]*\]'
- import pdb
- pdb.set_trace()
t.value = t.value.strip('[').strip(']')
return t
# Rule to track line numbers
def t_newline(t):
- r'\n+'
+ r'\n'
t.lexer.lineno += len(t.value)
t_ignore = ' \t'
@@ -70,14 +64,8 @@ def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
-#data='''
-#B[aa ]
-#W[AB]W[ab]
-#B[bb]
-#'''
+lexer = lex.lex() # Used by the parser
+#lexer = lex.lex(debug=True)
-lexer = lex.lex()
-#lexer.input(data)
-
-#for token in lexer:
-# print(token)
+if __name__ == '__main__':
+ lex.runmain()
diff --git a/imago/sgfParser/sgfyacc.py b/imago/sgfParser/sgfyacc.py
index e48e6b9..a07d179 100755
--- a/imago/sgfParser/sgfyacc.py
+++ b/imago/sgfParser/sgfyacc.py
@@ -43,14 +43,15 @@ def p_property_value(p):
p[1].addValue(p[2])
p[0] = p[1]
-def p_error(_):
+def p_error(p):
"""Error rule for syntax errors"""
- print("Syntax error in input!")
+ print("Syntax error in input - %s" % p)
-def main():
+# Build the parser
+parser = yacc.yacc()
+#parser = yacc.yacc(start='property')
- # Build the parser
- parser = yacc.yacc()
+def main():
s = ""
while True:
@@ -60,7 +61,7 @@ def main():
break
if not s:
continue
- result = parser.parse(s)
+ result = parser.parse(s, debug=True)
print(result.toString())
if __name__ == '__main__':
diff --git a/sgfyacc.py b/sgfyacc.py
deleted file mode 100755
index e48e6b9..0000000
--- a/sgfyacc.py
+++ /dev/null
@@ -1,67 +0,0 @@
-#!/usr/bin/python
-
-# --------------------------------------
-# sgyacc.py
-""" Parser for SGF """
-# --------------------------------------
-
-import ply.yacc as yacc
-
-from imago.sgfParser.sgflex import tokens
-from imago.sgfParser.astNode import ASTNode, Property
-
-def p_tree(p):
- '''tree : LPAREN node RPAREN
- | LPAREN tree RPAREN'''
- p[0] = p[2]
-
-def p_node_sequence(p):
- '''node : node node'''
- p[1].addToSequence(p[2])
- p[0] = p[1]
-
-def p_node_tree(p):
- '''node : node tree'''
- p[1].children.append(p[2])
- p[0] = p[1]
-
-def p_node(p):
- 'node : SCOLON'
- p[0] = ASTNode()
-
-def p_node_prop(p):
- 'node : node property'
- p[1].props[p[2].name] = p[2].value
- p[0] = p[1]
-
-def p_property(p):
- 'property : PROPID PROPVALUE'
- p[0] = Property(p[1], p[2])
-
-def p_property_value(p):
- 'property : property PROPVALUE'
- p[1].addValue(p[2])
- p[0] = p[1]
-
-def p_error(_):
- """Error rule for syntax errors"""
- print("Syntax error in input!")
-
-def main():
-
- # Build the parser
- parser = yacc.yacc()
-
- s = ""
- while True:
- try:
- s = input('calc > ')
- except EOFError:
- break
- if not s:
- continue
- result = parser.parse(s)
- print(result.toString())
-
-if __name__ == '__main__':
- main()