2 files changed, 31 insertions(+), 2 deletions(-)
viff/sfdl/grammar.py           |   15 +++++++++++++--
viff/test/sfdl/test_grammar.py |   18 ++++++++++++++++++


# HG changeset patch
# User Martin Geisler <[EMAIL PROTECTED]>
# Date 1227518457 -3600
# Node ID d6c03577b82c2d26f7f0827c0e51bc6d10f47b95
# Parent  6afba4921987626a0211d1861aecccce808c7b02
Constant declarations.

diff --git a/viff/sfdl/grammar.py b/viff/sfdl/grammar.py
--- a/viff/sfdl/grammar.py
+++ b/viff/sfdl/grammar.py
@@ -15,16 +15,27 @@
 # You should have received a copy of the GNU Lesser General Public
 # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
 
-from pyparsing import Suppress, Word, Keyword, \
-    cppStyleComment, alphas, alphanums
+from pyparsing import Suppress, Word, Keyword, ZeroOrMore, \
+    delimitedList, \
+    oneOf, operatorPrecedence, opAssoc, \
+    cppStyleComment, alphas, alphanums, nums
 
 class SFDLGrammar:
 
     def __init__(self):
+        SCOLON = Suppress(";")
+        EQUAL = Suppress("=")
         LCURLY, RCURLY = Suppress("{"), Suppress("}")
 
         self.ident = ident = Word(alphas + "_", alphanums + "_")
 
+        self.const_atom = const_atom \
+            = Word(nums) | delimitedList(ident, '.')
+        self.const_expr = const_expr \
+            = operatorPrecedence(const_atom, [(oneOf("+ -"), 2, opAssoc.LEFT)])
+        self.const_dec = const_dec \
+            = Keyword("const") + ident + EQUAL + const_expr + SCOLON
+
         self.program = program = Keyword("program") + ident \
             + LCURLY + RCURLY
         program.ignore(cppStyleComment)
diff --git a/viff/test/sfdl/test_grammar.py b/viff/test/sfdl/test_grammar.py
--- a/viff/test/sfdl/test_grammar.py
+++ b/viff/test/sfdl/test_grammar.py
@@ -65,5 +65,23 @@
         self.assertParse(self.grammar.program, src,
                          ['program', 'empty'])
 
+    def test_const_atom(self):
+        self.assertParse(self.grammar.const_atom, "123", ['123'])
+        self.assertParse(self.grammar.const_atom, "foo", ['foo'])
+        self.assertParse(self.grammar.const_atom, "x.y", ['x', 'y'])
+        self.assertParse(self.grammar.const_atom, "x.y.z", ['x', 'y', 'z'])
+
+    def test_const_expr(self):
+        self.assertParse(self.grammar.const_expr, "1 + 2", [['1', '+', '2']])
+        self.assertParse(self.grammar.const_expr, "1 + x", [['1', '+', 'x']])
+        self.assertParse(self.grammar.const_expr, "10 + (x - 20)",
+                         [['10', '+', ['x', '-', '20']]])
+
+    def test_const_dec(self):
+        self.assertParse(self.grammar.const_dec, "const x = 10;",
+                         ['const', 'x', '10'])
+        self.assertParse(self.grammar.const_dec, "const x = 10 + y;",
+                         ['const', 'x', ['10', '+', 'y']])
+
 if SFDLGrammar is None:
     TestGrammar.skip = "Could not import SFDLGrammar, missing pyparsing?"
_______________________________________________
viff-patches mailing list
viff-patches@viff.dk
http://lists.viff.dk/listinfo.cgi/viff-patches-viff.dk

Reply via email to