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

index 22c0f39..71831c1 100644 (file)
@@ -4,6 +4,7 @@ __pycache__
 /bootstrap/lex_yy_code.py
 /bootstrap/out
 /bootstrap/y_tab.py
+/element.py
 /lex-yacc-examples/*.c
 /lex-yacc-examples/*.h
 /lex-yacc-examples/*.o
@@ -23,6 +24,7 @@ __pycache__
 /tests/y_tab.py
 /tests/cal
 /tests/cal2
+/tests_ast/element.py
 /tests_ast/lex_yy.py
 /tests_ast/t_def.py
 /tests_ast/y_tab.py
index 0d393ca..6cb0404 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,7 @@
-all: lex_yy.py lex_yy_code.py t_def.py y_tab.py
+all: element.py lex_yy.py lex_yy_code.py t_def.py y_tab.py
+
+element.py: ../pitree.git/skel/element.py
+       cat $< >$@
 
 lex_yy.py: scan-gram.l
        bootstrap_pilex/pilex.py --element --python $<
@@ -7,10 +10,10 @@ lex_yy_code.py: scan-code.l
        bootstrap_pilex/pilex.py --element --python -o $@ $<
 
 t_def.py: piyacc.t
-       bootstrap_pitree/pitree.py --python $<
+       ../pitree.git/pitree.py --python $<
 
 y_tab.py: parse-gram.y
        bootstrap_piyacc/piyacc.py --element --python $<
 
 clean:
-       rm -f lex_yy.py lex_yy_code.py t_def.py y_tab.py
+       rm -f element.py lex_yy.py lex_yy_code.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
index 6d6b4e8..978568b 100644 (file)
--- a/piyacc.t
+++ b/piyacc.t
@@ -18,7 +18,6 @@
 
 %{
   import bisect_set
-  import element
   import lr1
   import sys
 %}
@@ -173,8 +172,8 @@ class AST {
 
 %%
 
-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(Item)
 def post_process(
index a342330..ea3c53c 100644 (file)
@@ -1,4 +1,7 @@
-all: lex_yy.py t_def.py y_tab.py
+all: element.py lex_yy.py t_def.py y_tab.py
+
+element.py: ../../pitree.git/skel/element.py
+       cat $< >$@
 
 lex_yy.py: cal_py.l
        ../../pilex.git/pilex.py --element --python $<
@@ -10,4 +13,4 @@ y_tab.py: cal_py.y
        ../piyacc.py --element --python $<
 
 clean:
-       rm -f lex_yy.py t_def.py y_tab.py
+       rm -f element.py lex_yy.py t_def.py y_tab.py
index 94870db..7516faa 100755 (executable)
 import t_def
 import element
 import sys
-import xml.etree.ElementTree
 import y_tab
 
 sys.stdout.write('Enter the expression: ')
 sys.stdout.flush()
 _ast = y_tab.yyparse(t_def.AST)
-xml.etree.ElementTree.dump(_ast)
-element.serialize(_ast, 'a.xml', 'utf-8')
-_ast = element.deserialize('a.xml', t_def.factory, 'utf-8')
+element.serialize(_ast, sys.stdout)
+#element.serialize(_ast, 'a.xml', 'utf-8')
+#_ast = element.deserialize('a.xml', t_def.factory, 'utf-8')
 for i in _ast:
   sys.stdout.write('{0:g}\n'.format(i.eval()))
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