/bootstrap/lex_yy.py
/bootstrap/out
/bootstrap/y_tab.py
+/element.py
/lex-yacc-examples/*.c
/lex-yacc-examples/*.h
/lex-yacc-examples/*.o
/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
-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
+++ /dev/null
-# 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
*/
%{
- import element
import nfa
import regex
import sys
%%
-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)
%%
+/* internal base class */
class Regex {
int n_groups = -1;
};
# 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):
-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 $<
../../pitree.git/pitree.py --python $<
clean:
- rm -f lex_yy.py t_def.py
+ rm -f element.py lex_yy.py t_def.py
%{
import t_def
-import xml.etree.ElementTree
NUM = 0x100
yylval = None
%}
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:
+++ /dev/null
-# 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