Commits

Olemis Lang committed 98c842c

GViz QL: Generate non-terminal precedence table from more readable representation

Comments (0)

Files changed (2)

trac-dev/gviz/tracgviz/testing/test_parsing.py

 
 from pygments.token import *
 
-from tracgviz.util.parsing import OperatorPrecedenceParser as Parser, Any, \
-    EndMarker, NonTerminal
+from tracgviz.util.parsing import Any, EndMarker, gen_precedence, NonTerminal, \
+    OperatorPrecedenceParser as Parser
 
 # Modified version of sample operator precedence grammar in
 # The Theory of Parsing, Translation, and Compiling, A.V. Aho, J.B. Ullman
       },
   }
 
-SAMPLE_GRAMMAR_PRECEDENCE = dict(
-    [ (tkn1, tkn2), prec] \
-        for tkn1, v1 in SAMPLE_GRAMMAR_PRECEDENCE.iteritems() \
-        for prec, v2 in v1.iteritems() \
-        for tkn2 in v2
-  )
+SAMPLE_GRAMMAR_PRECEDENCE = gen_precedence(SAMPLE_GRAMMAR_PRECEDENCE) 
 
 SAMPLE_GRAMMAR_PRODUCTIONS = {
     (Name, Any) : {

trac-dev/gviz/tracgviz/util/parsing.py

 # Token used to delimit segments in a token stream
 EndMarker   = Token.Grammar.EndMarker
 
+def gen_precedence(pseudo_precedence):
+  r"""Generates a precedence table from a more readable representation 
+  of the form 
+
+  {
+      SrcToken : {
+          Token.Grammar.Relationship.MorePrecedence : { DstToken1, ... }
+          Token.Grammar.Relationship.SamePrecedence : { DstToken2, ... }
+          Token.Grammar.Relationship.LessPrecedence : { DstToken3, ... }
+        },
+      ...
+  }
+  """
+  return dict(
+    [ (tkn1, tkn2), prec] \
+        for tkn1, v1 in pseudo_precedence.iteritems() \
+        for prec, v2 in v1.iteritems() \
+        for tkn2 in v2
+  )
+
+
 #------------------------------------------------------
 #   Exception classes
 #------------------------------------------------------