Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tree/node copying #57

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/fuzzer/HTMLGenerator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Generated by Grammarinator 19.3.post100+g8bc0117.d20220710
# Generated by Grammarinator 19.3.post103+ge23b086.d20221021

import itertools

from math import inf
from grammarinator.runtime import *


from copy import deepcopy


def html_space_serializer(root):

def _walk(node):
Expand Down Expand Up @@ -453,7 +456,7 @@ def htmlElement(self, parent=None):
self.TAG_OPEN(parent=current)
self.TAG_SLASH(parent=current)
self.htmlTagName(parent=current)
current.last_child = local_ctx['open_tag'].deepcopy()
current.last_child = deepcopy(local_ctx['open_tag'])
self.TAG_CLOSE(parent=current)
self._endOfHtmlElement()
elif choice == 1:
Expand Down
5 changes: 4 additions & 1 deletion examples/grammars/HTMLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ options { tokenVocab=HTMLLexer;
dot=any_unicode_char;}

@header {
from copy import deepcopy


def html_space_serializer(root):

def _walk(node):
Expand Down Expand Up @@ -72,7 +75,7 @@ htmlElements
;

htmlElement
: TAG_OPEN open_tag=htmlTagName htmlAttribute* TAG_CLOSE htmlContent TAG_OPEN TAG_SLASH htmlTagName {current.last_child = $open_tag.deepcopy()} TAG_CLOSE {self._endOfHtmlElement()}
: TAG_OPEN open_tag=htmlTagName htmlAttribute* TAG_CLOSE htmlContent TAG_OPEN TAG_SLASH htmlTagName {current.last_child = deepcopy($open_tag)} TAG_CLOSE {self._endOfHtmlElement()}
| TAG_OPEN open_tag=htmlTagName htmlAttribute* TAG_SLASH_CLOSE {self._endOfHtmlElement()}
| TAG_OPEN open_tag=htmlTagName htmlAttribute* TAG_CLOSE {self._endOfHtmlElement()}
| scriptlet
Expand Down
15 changes: 7 additions & 8 deletions grammarinator/runtime/tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017-2021 Renata Hodovan, Akos Kiss.
# Copyright (c) 2017-2022 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
Expand All @@ -7,7 +7,7 @@

import pickle

from copy import copy, deepcopy
from copy import deepcopy
from math import inf


Expand All @@ -19,6 +19,11 @@ def __init__(self, root):
self.root = root
self.node_dict = None

def __deepcopy__(self, memo):
tree = Tree(deepcopy(self.root, memo=memo))
tree.annotate()
return tree

def annotate(self):
"""
Add a dictionary to the current object which is indexed by node names
Expand Down Expand Up @@ -101,12 +106,6 @@ def __iadd__(self, child):
self.add_child(child)
return self

def copy(self):
return copy(self)

def deepcopy(self):
return deepcopy(self)

@property
def left_sibling(self):
try:
Expand Down
5 changes: 3 additions & 2 deletions tests/grammars/Custom.g4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2020 Renata Hodovan, Akos Kiss.
* Copyright (c) 2017-2022 Renata Hodovan, Akos Kiss.
*
* Licensed under the BSD 3-Clause License
* <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
Expand Down Expand Up @@ -33,6 +33,7 @@
grammar Custom;

@lexer::header {
from copy import deepcopy
from sys import platform as CustomLexerPlatform
}

Expand All @@ -56,7 +57,7 @@ start
;

tag
: '<' remember=tagname '>' CONTENT '</' tagname {current.last_child = $remember.deepcopy()} '>'
: '<' remember=tagname '>' CONTENT '</' tagname {current.last_child = deepcopy($remember)} '>'
;

tagname
Expand Down