Move CodeBlocks class inside new Section1Or2 class (formerly SectionCodeBlocks),...
authorNick Downing <downing.nick@gmail.com>
Mon, 2 Jul 2018 07:48:39 +0000 (17:48 +1000)
committerNick Downing <downing.nick@gmail.com>
Mon, 2 Jul 2018 07:51:24 +0000 (17:51 +1000)
ast.py
flex_dfa.py
skel/lex.yy.c.patch
tests/Makefile

diff --git a/ast.py b/ast.py
index af8f59d..f8417ac 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -179,11 +179,48 @@ class PLex(element.Element):
       return 'ast.PLex.Name({0:s})'.format(', '.join(params))
     # GENERATE END
 
-  class SectionCodeBlocks(Section):
+  class Section1Or2(Section):
+    class CodeBlock(Item):
+      # GENERATE ELEMENT() BEGIN
+      def __init__(
+        self,
+        tag = 'PLex_Section1Or2_CodeBlock',
+        attrib = {},
+        text = '',
+        children = []
+      ):
+        Item.__init__(
+          self,
+          tag,
+          attrib,
+          text,
+          children
+        )
+      def copy(self, factory = None):
+        result = Item.copy(
+          self,
+          CodeBlock if factory is None else factory
+        )
+        return result
+      def __repr__(self):
+        params = []
+        self.repr_serialize(params)
+        return 'ast.PLex.Section1Or2.CodeBlock({0:s})'.format(', '.join(params))
+      # GENERATE END
+      def post_process(
+        self,
+        plex,
+        section,
+        name_to_start_condition,
+        all_start_conditions,
+        inclusive_start_conditions
+      ):
+        section.code_blocks.append(self)
+
     # GENERATE ELEMENT(list(ref) code_blocks) BEGIN
     def __init__(
       self,
-      tag = 'PLex_SectionCodeBlocks',
+      tag = 'PLex_Section1Or2',
       attrib = {},
       text = '',
       children = [],
@@ -212,7 +249,7 @@ class PLex(element.Element):
     def copy(self, factory = None):
       result = PLex.Section.copy(
         self,
-        SectionCodeBlocks if factory is None else factory
+        Section1Or2 if factory is None else factory
       )
       result.code_blocks = self.code_blocks
       return result
@@ -227,47 +264,10 @@ class PLex(element.Element):
     def __repr__(self):
       params = []
       self.repr_serialize(params)
-      return 'ast.PLex.SectionCodeBlocks({0:s})'.format(', '.join(params))
-    # GENERATE END
-
-  class CodeBlock(Item):
-    # GENERATE ELEMENT() BEGIN
-    def __init__(
-      self,
-      tag = 'PLex_CodeBlock',
-      attrib = {},
-      text = '',
-      children = []
-    ):
-      Item.__init__(
-        self,
-        tag,
-        attrib,
-        text,
-        children
-      )
-    def copy(self, factory = None):
-      result = Item.copy(
-        self,
-        CodeBlock if factory is None else factory
-      )
-      return result
-    def __repr__(self):
-      params = []
-      self.repr_serialize(params)
-      return 'ast.PLex.CodeBlock({0:s})'.format(', '.join(params))
+      return 'ast.PLex.Section1Or2({0:s})'.format(', '.join(params))
     # GENERATE END
-    def post_process(
-      self,
-      plex,
-      section,
-      name_to_start_condition,
-      all_start_conditions,
-      inclusive_start_conditions
-    ):
-      section.code_blocks.append(self)
 
-  class Section1(SectionCodeBlocks):
+  class Section1(Section1Or2):
     class Options(Item):
       class Option(element.Element):
         # GENERATE ELEMENT() BEGIN
@@ -628,7 +628,7 @@ class PLex(element.Element):
       yymore = False,
       yywrap = False
     ):
-      PLex.SectionCodeBlocks.__init__(
+      PLex.Section1Or2.__init__(
         self,
         tag,
         attrib,
@@ -662,21 +662,21 @@ class PLex(element.Element):
         yywrap
       )
     def serialize(self, ref_list, indent = 0):
-      PLex.SectionCodeBlocks.serialize(self, ref_list, indent)
+      PLex.Section1Or2.serialize(self, ref_list, indent)
       self.set('ecs', element.serialize_bool(self.ecs))
       self.set('meta_ecs', element.serialize_bool(self.meta_ecs))
       self.set('reject', element.serialize_bool(self.reject))
       self.set('yymore', element.serialize_bool(self.yymore))
       self.set('yywrap', element.serialize_bool(self.yywrap))
     def deserialize(self, ref_list):
-      PLex.SectionCodeBlocks.deserialize(self, ref_list)
+      PLex.Section1Or2.deserialize(self, ref_list)
       self.ecs = element.deserialize_bool(self.get('ecs', 'false'))
       self.meta_ecs = element.deserialize_bool(self.get('meta_ecs', 'false'))
       self.reject = element.deserialize_bool(self.get('reject', 'false'))
       self.yymore = element.deserialize_bool(self.get('yymore', 'false'))
       self.yywrap = element.deserialize_bool(self.get('yywrap', 'false'))
     def copy(self, factory = None):
-      result = PLex.SectionCodeBlocks.copy(
+      result = PLex.Section1Or2.copy(
         self,
         Section1 if factory is None else factory
       )
@@ -687,7 +687,7 @@ class PLex(element.Element):
       result.yywrap = self.yywrap
       return result
     def repr_serialize(self, params):
-      PLex.SectionCodeBlocks.repr_serialize(self, params)
+      PLex.Section1Or2.repr_serialize(self, params)
       if self.ecs != False:
         params.append(
           'ecs = {0:s}'.format(repr(self.ecs))
@@ -733,7 +733,7 @@ class PLex(element.Element):
         inclusive_start_conditions
       )
 
-  class Section2(SectionCodeBlocks):
+  class Section2(Section1Or2):
     class StartConditions(element.Element):
       # GENERATE ELEMENT(bool wildcard) BEGIN
       def __init__(
@@ -965,7 +965,7 @@ class PLex(element.Element):
       children = [],
       code_blocks = []
     ):
-      PLex.SectionCodeBlocks.__init__(
+      PLex.Section1Or2.__init__(
         self,
         tag,
         attrib,
@@ -974,7 +974,7 @@ class PLex(element.Element):
         code_blocks
       )
     def copy(self, factory = None):
-      result = PLex.SectionCodeBlocks.copy(
+      result = PLex.Section1Or2.copy(
         self,
         Section2 if factory is None else factory
       )
@@ -1172,8 +1172,8 @@ tag_to_class = {
   'PLex_StartCondition': PLex.StartCondition,
   'PLex_Section': PLex.Section,
   'PLex_Name': PLex.Name,
-  'PLex_SectionCodeBlocks': PLex.SectionCodeBlocks,
-  'PLex_CodeBlock': PLex.CodeBlock,
+  'PLex_Section1Or2': PLex.Section1Or2,
+  'PLex_Section1Or2_CodeBlock': PLex.Section1Or2.CodeBlock,
   'PLex_Section1': PLex.Section1,
   'PLex_Section1_Options': PLex.Section1.Options,
   'PLex_Section1_Options_Option': PLex.Section1.Options.Option,
index 37046e8..04bbe3e 100644 (file)
@@ -205,28 +205,16 @@ def generate(plex, skel_file, out_file):
     with open(out_file, 'w+') as fout:
       line = fin.readline()
       while len(line):
-        if line == '/* GENERATE SECTION1 */\n':
+        if line == '/* GENERATE YYWRAP */\n':
           fout.write(
-            '''/* GENERATE SECTION1 BEGIN */
-{0:s}/* GENERATE SECTION1 END*/
-'''.format(
-              ''.join([element.get_text(i, 0) for i in plex[0].code_blocks])
-            )
-          )
-        elif line == '/* GENERATE STARTCONDDECL */\n':
-          fout.write(
-            '''/* GENERATE STARTCONDDECL BEGIN */
-{0:s}/* GENERATE STARTCONDDECL END*/
+            '''/* GENERATE YYWRAP BEGIN */
+{0:s}/* GENERATE YYWRAP END */
 '''.format(
-              ''.join(
-                [
-                  '#define {0:s} {1:d}\n'.format(
-                    plex.start_conditions[i].name,
-                    i
-                  )
-                  for i in range(len(plex.start_conditions))
-                ]
-              )
+              ''
+            if plex[0].yywrap else
+              '''#define yywrap() (/*CONSTCOND*/1)
+#define YY_SKIP_YYWRAP
+'''
             )
           )
         elif line == '/* GENERATE TABLES */\n':
@@ -333,6 +321,30 @@ static const flex_int16_t yy_chk[] = {{{6:s}
               )
             )
           )
+        elif line == '/* GENERATE SECTION1 */\n':
+          fout.write(
+            '''/* GENERATE SECTION1 BEGIN */
+{0:s}/* GENERATE SECTION1 END */
+'''.format(
+              ''.join([element.get_text(i, 0) for i in plex[0].code_blocks])
+            )
+          )
+        elif line == '/* GENERATE STARTCONDDECL */\n':
+          fout.write(
+            '''/* GENERATE STARTCONDDECL BEGIN */
+{0:s}/* GENERATE STARTCONDDECL END*/
+'''.format(
+              ''.join(
+                [
+                  '#define {0:s} {1:d}\n'.format(
+                    plex.start_conditions[i].name,
+                    i
+                  )
+                  for i in range(len(plex.start_conditions))
+                ]
+              )
+            )
+          )
         elif line == '/* GENERATE SECTION2INITIAL */\n':
           fout.write(
             '''/* GENERATE SECTION2INITIAL BEGIN */
index ee49c9b..a4296d4 100644 (file)
@@ -1,5 +1,5 @@
---- lex.yy.c.orig      2018-06-29 12:12:25.644004319 +1000
-+++ lex.yy.c   2018-06-30 09:47:14.740034867 +1000
+--- lex.yy.c.orig      2018-07-02 15:42:45.835849543 +1000
++++ lex.yy.c   2018-07-02 15:43:03.847849811 +1000
 @@ -1,6 +1,3 @@
 -
 -#line 2 "lex.yy.c"
@@ -7,7 +7,15 @@
  #define  YY_INT_ALIGNED short int
  
  /* A lexical scanner generated by flex */
-@@ -351,179 +348,8 @@
+@@ -321,6 +318,7 @@
+       }
+ #define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol)
++/* GENERATE YYWRAP */
+ typedef flex_uint8_t YY_CHAR;
+ FILE *yyin = NULL, *yyout = NULL;
+@@ -351,179 +349,8 @@
        (yy_hold_char) = *yy_cp; \
        *yy_cp = '\0'; \
        (yy_c_buf_p) = yy_cp;
  
  extern int yy_flex_debug;
  int yy_flex_debug = 0;
-@@ -553,10 +379,10 @@
+@@ -553,10 +380,10 @@
  #define YY_MORE_ADJ (yy_more_len)
  #define YY_RESTORE_YY_MORE_OFFSET
  char *yytext;
  
  #ifndef YY_NO_UNISTD_H
  /* Special case for "unistd.h", since it is non-ANSI. We include it way
-@@ -780,9 +606,7 @@
+@@ -780,9 +607,7 @@
                }
  
        {
  
        while ( /*CONSTCOND*/1 )                /* loops until end-of-file is reached */
                {
-@@ -820,7 +644,7 @@
+@@ -820,7 +645,7 @@
                        *(yy_state_ptr)++ = yy_current_state;
                        ++yy_cp;
                        }
  
  yy_find_action:
                yy_current_state = *--(yy_state_ptr);
-@@ -828,7 +652,7 @@
+@@ -828,7 +653,7 @@
  find_rule: /* we branch to this label when backing up */
                for ( ; ; ) /* until we find what rule we matched */
                        {
                                {
                                yy_act = yy_acclist[(yy_lp)];
                                if ( yy_act & YY_TRAILING_HEAD_MASK ||
-@@ -870,24 +694,7 @@
+@@ -870,24 +695,7 @@
  
                switch ( yy_act )
        { /* beginning of action switch */
  
        case YY_END_OF_BUFFER:
                {
-@@ -1862,4 +1669,4 @@
+@@ -1862,4 +1670,4 @@
  
  #define YYTABLES_NAME "yytables"
  
index 323dce6..b01e5d0 100644 (file)
@@ -1,8 +1,7 @@
 all: cal flex0 flex1
 
 cal: y.tab.c lex.yy.c
-       # remove -ll when we've implemented noyywrap
-       gcc -o $@ $< -ll
+       gcc -o $@ $<
 
 lex.yy.c: cal.l
        ../../bootstrap_flex.git/src/flex -o /dev/null $< 2>$<.xml