Update to pitree.git commit 5cba525 (don't use bootstrap_pitree for now)
authorNick Downing <nick@ndcode.org>
Tue, 29 Jan 2019 01:06:42 +0000 (12:06 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 29 Jan 2019 02:41:48 +0000 (13:41 +1100)
.gitignore
Makefile
element.py [deleted file]
pilex.t
regex.t
tests_ast/Makefile
tests_ast/cal_py.l
tests_ast/element.py [deleted file]

index 325aeaf..c94ec9c 100644 (file)
@@ -3,6 +3,7 @@ __pycache__
 /bootstrap/lex_yy.py
 /bootstrap/out
 /bootstrap/y_tab.py
+/element.py
 /lex-yacc-examples/*.c
 /lex-yacc-examples/*.h
 /lex-yacc-examples/*.o
@@ -21,6 +22,7 @@ __pycache__
 /tests/flex0
 /tests/flex1
 /tests/lex_yy.py
+/tests_ast/element.py
 /tests_ast/lex_yy.py
 /tests_ast/t_def.py
 /y_tab.py
index 9504f80..63ca838 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,16 +1,19 @@
-all: lex_yy.py regex.py t_def.py y_tab.py
+all: element.py lex_yy.py regex.py t_def.py y_tab.py
+
+element.py: ../pitree.git/skel/element.py
+       cat $< >$@
 
 lex_yy.py: scan.l
        bootstrap_pilex/pilex.py --element --python $<
 
 regex.py: regex.t
-       bootstrap_pitree/pitree.py --python -o $@ $<
+       ../pitree.git/pitree.py --python -o $@ $<
 
 t_def.py: pilex.t
-       bootstrap_pitree/pitree.py --python $<
+       ../pitree.git/pitree.py --python $<
 
 y_tab.py: parse.y
        bootstrap_piyacc/piyacc.py --element --python $<
 
 clean:
-       rm -f lex_yy.py regex.py t_def.py y_tab.py
+       rm -f element.py lex_yy.py regex.py t_def.py y_tab.py
diff --git a/element.py b/element.py
deleted file mode 100644 (file)
index e8732cc..0000000
+++ /dev/null
@@ -1,172 +0,0 @@
-# Copyright (C) 2019 Nick Downing <nick@ndcode.org>
-# SPDX-License-Identifier: GPL-2.0-only
-#
-# This program is free software; you can redistribute it and/or modify it under
-# the terms of the GNU General Public License as published by the Free Software
-# Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
-# details.
-#
-# You should have received a copy of the GNU General Public License along with
-# this program; if not, write to the Free Software Foundation, Inc., 51
-# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
-
-import xml.etree.ElementTree
-
-class Element(xml.etree.ElementTree._Element_Py):
-  def __init__(self, tag = 'Element', attrib = {}, text = '', children = []):
-    xml.etree.ElementTree._Element_Py.__init__(self, tag, attrib)
-    self.ref = -1
-    self.seen = False
-    set_text(self, 0, text)
-    self[:] = children
-  def serialize(self, ref_list):
-    for i in self:
-      # parented, enforce that child can only be parented at most once
-      # (although there can be unlimited numbers of numeric refs to it)
-      assert not i.seen
-      i.seen = True
-      if i.ref == -1:
-        i.serialize(ref_list)
-  def deserialize(self, ref_list):
-    for i in self:
-      i.deserialize(ref_list)
-  def copy(self, factory = None):
-    result = (Element if factory is None else factory)(self.tag, self.attrib)
-    result.text = self.text
-    result.tail = self.tail
-    result[:] = [i.copy() for i in self]
-    return result
-
-bool_to_str = ['false', 'true']
-def serialize_bool(value):
-  return bool_to_str[int(value)]
-
-str_to_bool = {'false': False, 'true': True}
-def deserialize_bool(text):
-  assert text is not None
-  return str_to_bool[text]
-
-def serialize_int(value):
-  return str(value)
-
-def deserialize_int(text):
-  assert text is not None
-  return int(text)
-
-def serialize_ref(value, ref_list):
-  if value is None:
-    ref = -1
-  else:
-    ref = value.ref
-    if ref == -1:
-      ref = len(ref_list)
-      ref_list.append(value)
-      value.ref = ref
-      value.set('ref', str(ref))
-      # this doesn't set the seen flag, so it will be parented by the
-      # root, unless it is already parented or gets parented later on
-      if not value.seen:
-        value.serialize(ref_list)
-  return str(ref)
-
-def deserialize_ref(text, ref_list):
-  assert text is not None
-  ref = int(text)
-  return None if ref < 0 else ref_list[ref]
-
-def serialize_str(value):
-  return value
-
-def deserialize_str(text):
-  assert text is not None
-  return text
-
-def serialize(value, fout, encoding = 'unicode'):
-  ref_list = []
-  serialize_ref(value, ref_list)
-  parents = [i for i in ref_list if not i.seen]
-  root = Element('root', children = parents)
-  for i in range(len(root)):
-    set_text(root, i, '\n  ')
-  set_text(root, len(root), '\n')
-  root.tail = '\n'
-  xml.etree.ElementTree.ElementTree(root).write(fout, encoding)
-  for i in root:
-    i.tail = None
-  for i in ref_list:
-    i.ref = -1
-    del i.attrib['ref']
-  i = 0
-  while i < len(parents):
-    for j in parents[i]:
-      j.seen = False
-      parents.append(j)
-    i += 1
-
-def deserialize(fin, factory = Element, encoding = 'unicode'):
-  root = xml.etree.ElementTree.parse(
-    fin,
-    xml.etree.ElementTree.XMLParser(
-      target = xml.etree.ElementTree.TreeBuilder(factory),
-      encoding = encoding
-    )
-  ).getroot()
-  assert root.tag == 'root'
-  for i in root:
-    i.tail = None
-  i = 0
-  parents = root[:]
-  ref_list = []
-  while i < len(parents):
-    j = parents[i]
-    if 'ref' in j.attrib:
-      ref = int(j.attrib['ref'])
-      del j.attrib['ref']
-      if len(ref_list) < ref + 1:
-        ref_list.extend([None] * (ref + 1 - len(ref_list)))
-      ref_list[ref] = j
-    parents.extend(j[:])
-    i += 1
-  for i in root:
-    i.deserialize(ref_list)
-  return ref_list[0]
-
-# compatibility scheme to access arbitrary xml.etree.ElementTree.Element-like
-# objects (not just Element defined above) using a more consistent interface:
-def get_text(root, i):
-  if i < 0:
-    i += len(root) + 1
-  text = root.text if i == 0 else root[i - 1].tail
-  return '' if text is None else text
-
-def set_text(root, i, text):
-  if i < 0:
-    i += len(root) + 1
-  if len(text) == 0:
-    text = None
-  if i == 0:
-    root.text = text
-  else:
-    root[i - 1].tail = text
-
-def to_text(root):
-  return ''.join(
-    [
-      j
-      for i in range(len(root))
-      for j in [get_text(root, i), to_text(root[i])]
-    ] +
-    [get_text(root, len(root))]
-  )
-
-def concatenate(children, factory = Element, *args, **kwargs):
-  root = factory(*args, **kwargs)
-  for child in children:
-    i = len(root)
-    set_text(root, i, get_text(root, i) + get_text(child, 0))
-    root[i:] = child[:]
-  return root
diff --git a/pilex.t b/pilex.t
index 410f374..b723c63 100644 (file)
--- a/pilex.t
+++ b/pilex.t
@@ -17,7 +17,6 @@
  */
 
 %{
-  import element
   import nfa
   import regex
   import sys
@@ -220,7 +219,7 @@ class AST {
 
 %%
 
-def factory(tag, attrib = {}, *args, **kwargs):
+def factory(tag, *args, **kwargs):
   return tag_to_class.get(tag, regex.factory)(tag, attrib, *args, **kwargs)
 
 @method(Item)
diff --git a/regex.t b/regex.t
index 7e389c9..542e63e 100644 (file)
--- a/regex.t
+++ b/regex.t
@@ -24,6 +24,7 @@
 
 %%
 
+/* internal base class */
 class Regex {
   int n_groups = -1;
 };
@@ -62,10 +63,8 @@ class RegexGroupElement: RegexGroup {
 # defines the alphabet size, set this to 0x11000 for unicode
 n_characters = 0x100
 
-# internal base class
-
-def factory(tag, attrib = {}, *args, **kwargs):
-  return tag_to_class.get(tag, element.Element)(tag, attrib, *args, **kwargs)
+def factory(tag, *args, **kwargs):
+  return tag_to_class.get(tag, element.Element)(tag, *args, **kwargs)
 
 @method(Regex)
 def post_process(self, groups, caseless = False):
index cd9f81a..6b5529a 100644 (file)
@@ -1,4 +1,7 @@
-all: lex_yy.py t_def.py
+all: element.py lex_yy.py t_def.py
+
+element.py: ../../pitree.git/skel/element.py
+       cat $< >$@
 
 lex_yy.py: cal_py.l
        ../pilex.py --element --python $<
@@ -7,4 +10,4 @@ t_def.py: cal_py.t
        ../../pitree.git/pitree.py --python $<
 
 clean:
-       rm -f lex_yy.py t_def.py
+       rm -f element.py lex_yy.py t_def.py
index b7cbe33..0fcc85f 100644 (file)
@@ -1,6 +1,5 @@
 %{
 import t_def
-import xml.etree.ElementTree
 NUM = 0x100 
 yylval = None
 %}
@@ -27,9 +26,9 @@ if __name__ == '__main__':
   token = yylex()
   while token != 0:
     print('yy_element_space')
-    xml.etree.ElementTree.dump(yy_element_space)
+    element.serialize(yy_element_space, sys.stdout)
     print('yy_element_token')
-    xml.etree.ElementTree.dump(yy_element_token)
+    element.serialize(yy_element_token, sys.stdout)
     if token == NUM:
       print('NUM', yylval)
     else:
diff --git a/tests_ast/element.py b/tests_ast/element.py
deleted file mode 100644 (file)
index 6de5806..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-# Copyright (C) 2019 Nick Downing <nick@ndcode.org>
-# SPDX-License-Identifier: GPL-2.0-only
-#
-# This program is free software; you can redistribute it and/or modify it under
-# the terms of the GNU General Public License as published by the Free Software
-# Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
-# details.
-#
-# You should have received a copy of the GNU General Public License along with
-# this program; if not, write to the Free Software Foundation, Inc., 51
-# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
-
-import xml.etree.ElementTree
-
-class Element(xml.etree.ElementTree._Element_Py):
-  def __init__(self, tag = 'Element', attrib = {}, text = '', children = []):
-    xml.etree.ElementTree._Element_Py.__init__(self, tag, attrib)
-    self.ref = -1
-    self.seen = False
-    set_text(self, 0, text)
-    self[:] = children
-  def serialize(self, ref_list):
-    for i in self:
-      # parented, enforce that child can only be parented at most once
-      # (although there can be unlimited numbers of numeric refs to it)
-      assert not i.seen
-      i.seen = True
-      if i.ref == -1:
-        i.serialize(ref_list)
-  def deserialize(self, ref_list):
-    for i in self:
-      i.deserialize(ref_list)
-  def copy(self, factory = None):
-    result = (Element if factory is None else factory)(self.tag, self.attrib)
-    result.text = self.text
-    result.tail = self.tail
-    result[:] = [i.copy() for i in self]
-    return result
-  def repr_serialize(self, params):
-    if len(self):
-      params.append(
-        'children = [{0:s}]'.format(
-          ', '.join([repr(i) for i in self])
-        )
-      )
-  def __repr__(self):
-    params = []
-    self.repr_serialize(params)
-    return 'element.Element({0:s})'.format(', '.join(params))
-
-bool_to_str = ['false', 'true']
-def serialize_bool(value):
-  return bool_to_str[int(value)]
-
-str_to_bool = {'false': False, 'true': True}
-def deserialize_bool(text):
-  assert text is not None
-  return str_to_bool[text]
-
-def serialize_int(value):
-  return str(value)
-
-def deserialize_int(text):
-  assert text is not None
-  return int(text)
-
-def serialize_ref(value, ref_list):
-  assert text is not None
-  if value is None:
-    ref = -1
-  else:
-    ref = value.ref
-    if ref == -1:
-      ref = len(ref_list)
-      ref_list.append(value)
-      value.ref = ref
-      value.set('ref', str(ref))
-      # this doesn't set the seen flag, so it will be parented by the
-      # root, unless it is already parented or gets parented later on
-      if not value.seen:
-        value.serialize(ref_list)
-  return str(ref)
-
-def deserialize_ref(text, ref_list):
-  assert text is not None
-  ref = int(text)
-  return None if ref < 0 else ref_list[ref]
-
-def serialize_str(value):
-  return value
-
-def deserialize_str(text):
-  assert text is not None
-  return text
-
-def serialize(value, fout, encoding = 'unicode'):
-  ref_list = []
-  serialize_ref(value, ref_list)
-  parents = [i for i in ref_list if not i.seen]
-  root = Element('root', children = parents)
-  for i in range(len(root)):
-    set_text(root, i, '\n  ')
-  set_text(root, len(root), '\n')
-  root.tail = '\n'
-  xml.etree.ElementTree.ElementTree(root).write(fout, encoding)
-  for i in root:
-    i.tail = None
-  for i in ref_list:
-    i.ref = -1
-    del i.attrib['ref']
-  i = 0
-  while i < len(parents):
-    for j in parents[i]:
-      j.seen = False
-      parents.append(j)
-    i += 1
-
-def deserialize(fin, factory = Element, encoding = 'unicode'):
-  root = xml.etree.ElementTree.parse(
-    fin,
-    xml.etree.ElementTree.XMLParser(
-      target = xml.etree.ElementTree.TreeBuilder(factory),
-      encoding = encoding
-    )
-  ).getroot()
-  assert root.tag == 'root'
-  for i in root:
-    i.tail = None
-  i = 0
-  parents = root[:]
-  ref_list = []
-  while i < len(parents):
-    j = parents[i]
-    if 'ref' in j.attrib:
-      ref = int(j.attrib['ref'])
-      del j.attrib['ref']
-      if len(ref_list) < ref + 1:
-        ref_list.extend([None] * (ref + 1 - len(ref_list)))
-      ref_list[ref] = j
-    parents.extend(j[:])
-    i += 1
-  for i in root:
-    i.deserialize(ref_list)
-  return ref_list[0]
-
-# compatibility scheme to access arbitrary xml.etree.ElementTree.Element-like
-# objects (not just Element defined above) using a more consistent interface:
-def get_text(root, i):
-  if i < 0:
-    i += len(root) + 1
-  text = root.text if i == 0 else root[i - 1].tail
-  return '' if text is None else text
-
-def set_text(root, i, text):
-  if i < 0:
-    i += len(root) + 1
-  if len(text) == 0:
-    text = None
-  if i == 0:
-    root.text = text
-  else:
-    root[i - 1].tail = text
-
-def to_text(root):
-  return ''.join(
-    [
-      j
-      for i in range(len(root))
-      for j in [get_text(root, i), to_text(root[i])]
-    ] +
-    [get_text(root, len(root))]
-  )
-
-def concatenate(children, factory = Element, *args, **kwargs):
-  root = factory(*args, **kwargs)
-  for child in children:
-    i = len(root)
-    set_text(root, i, get_text(root, i) + get_text(child, 0))
-    root[i:] = child[:]
-  return root