Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 9 additions & 45 deletions gram/create_parse_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import sys
import os


def camel_case(s):
s = sub(r"(_|-)+", " ", s).title().replace(" ", "")
return ''.join([s[0].upper(), s[1:]])
from util import camel_case, remove_opt, write_to_file


diag_kinds = {}
Expand Down Expand Up @@ -262,8 +259,14 @@ def create_parallel_function(serial_idx, key, v):
this->calling_depth(),
Line::segments_type()
}};
line.segments_.append(non_recursive_output.line_);
line.segments_.append(output.line_);
lps_assert({tag_name}, !non_recursive_output.line_->segments_.empty());
for(const auto&l : non_recursive_output.line_->segments_){{
line.segments_.append(l);
}}
lps_assert({tag_name}, !output.line_->segments_.empty());
for(const auto&l : output.line_->segments_){{
line.segments_.append(l);
}}
output.line_ = context_->paint(line);
for (const auto& a : recursive_funcs_1.valid_outputs()) {{
this->context_->save(this->cur_token(), a.cur_token_, this->kind(),
Expand All @@ -285,45 +288,6 @@ def create_parallel_function(serial_idx, key, v):
return content_op, "", content_type, "", flg


def remove_opt(s_: str):
s = s_
if s_.endswith("[opt]"):
s = s_.replace("[opt]", "")
return s, s != s_


def write_to_file(src_file_name, the_contents, title, end="}"):
with open(src_file_name, "w") as f:
f.write(title + the_contents + end)
encoding_py3 = {}
if sys.version_info[0] >= 3:
encoding_py3['encoding'] = 'utf-8'

try:
invocation = ["clang-format", src_file_name,
"-i", f'--style=file:{os.path.dirname(__file__)}/../.clang-format']

proc = subprocess.Popen(
invocation,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
**encoding_py3)
except OSError as exc:
print("error.")
assert (False)
proc_stdout = proc.stdout
proc_stderr = proc.stderr
outs = list(proc_stdout.readlines())
errs = list(proc_stderr.readlines())
proc.wait()

if proc.returncode:
print("error.", subprocess.list2cmdline(
invocation), proc.returncode, errs)
assert (False)


if __name__ == "__main__":
args = sys.argv[1:]
all_content_out_path = args[0]
Expand Down
166 changes: 166 additions & 0 deletions gram/create_semantic_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#
# MIT License
# Copyright (c) 2023 mxlol233 (mxlol233@outlook.com)

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

from config import gram_tree, sp_tokens
from re import sub
import subprocess
import sys
import os

from util import camel_case, remove_opt, write_to_file, underline_case


if __name__ == "__main__":
args = sys.argv[1:]
unit_h_out_path = args[0]

unit_h_out_template = """/*
* MIT License
* Copyright (c) 2023 mxlol233 (mxlol233@outlook.com)

* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:

* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once

#include "sema.h"

namespace lps::sema::details::unit {

__CONTENT_UNIT_DECL__

__CONTENT_UNIT_DEF__

}
"""

unit_def_template = """
class __NAME__ : public Unit, public HasElements {

public:
explicit __NAME__(const parser::details::Tree::Node* gram_node, basic::Vector<2, std::unique_ptr<Unit>>&& elements):Unit(gram_node), HasElements(std::move(elements)){{
this->kind_ = parser::details::ParseFunctionKind::k__NAME__;
}}
void build() override { LPS_ERROR(kTag, "not implemented"); }
void check() override { LPS_ERROR(kTag, "not implemented"); }
~__NAME__() override = default;

private:
constexpr static const char* kTag = "lps::sema::details::__NAME__";
};
"""

case_template = f"""
case parser::details::ParseFunctionKind::k__NAME__: {{
return std::make_unique<details::unit::__NAME__>(&node,
std::move(elements));
break;
}}
"""

str_unit_decl = ""
str_unit_def_all = ""
case_strs = ""

for k, v in gram_tree.items():
name = camel_case(k)



str_type = ""
str_member = ""

if len(v) == 0:
# not valid semantic unit
pass
elif isinstance(v[0], str):
# only one serial
str_type += f"""struct Type{{"""
str_type += "\n"
for idx, s_ in enumerate(v):
assert isinstance(s_, str)
s, is_opt = remove_opt(s_)
if s.startswith('`'):

str_type += f""" std::unique_ptr<Symbol> s{idx}_{{nullptr}};"""


else:
k_name = camel_case(s)
k_underline_name = underline_case(s)
str_type += f""" std::unique_ptr<{k_name}> s{idx}_{{nullptr}};"""
str_type += "\n"
str_type += f"""}};"""
str_member += f"""Type ele_; """
else:
str_member = f"""union {{"""
for ii,tv in enumerate(v):
str_type += f"""struct Type{ii}{{"""
str_type += "\n"
for idx, s_ in enumerate(tv):
assert isinstance(s_, str)
s, is_opt = remove_opt(s_)
if s.startswith('`'):

str_type += f""" std::unique_ptr<Symbol> s{idx}_{{nullptr}};"""


else:
k_name = camel_case(s)
k_underline_name = underline_case(s)
str_type += f""" std::unique_ptr<{k_name}> s{idx}_{{nullptr}};"""
str_type += "\n"
str_type += f"""}};"""
str_member += f"""Type{ii} v{ii}_; """

str_member += f"""}} ele_;"""

str_unit_decl += f"""class {name};"""
str_unit_decl += "\n"
str_unit_def = unit_def_template.replace("__TYPE__", str_type)
str_unit_def = str_unit_def.replace("__MEMBER__", str_member)
str_unit_def = str_unit_def.replace("__NAME__", name)
case_strs += case_template.replace("__NAME__", name)
str_unit_def_all += str_unit_def

contents = unit_h_out_template.replace("__CONTENT_UNIT_DECL__", str_unit_decl)
contents = contents.replace("__CONTENT_UNIT_DEF__", str_unit_def_all)


write_to_file(unit_h_out_path, contents, "\n", "\n")
write_to_file(unit_h_out_path + ".case", case_strs, "\n", "\n")
74 changes: 74 additions & 0 deletions gram/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# MIT License
# Copyright (c) 2023 mxlol233 (mxlol233@outlook.com)

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

from re import sub
import subprocess
import sys
import os


def camel_case(s):
s = sub(r"(_|-)+", " ", s).title().replace(" ", "")
return ''.join([s[0].upper(), s[1:]])

def underline_case(s):
s = s.replace("-", "_").replace(" ", "_")
return ''.join([s[0].lower(), s[1:]]) + "_"

def remove_opt(s_: str):
s = s_
if s_.endswith("[opt]"):
s = s_.replace("[opt]", "")
return s, s != s_


def write_to_file(src_file_name, the_contents, title, end="}"):
with open(src_file_name, "w") as f:
f.write(title + the_contents + end)
encoding_py3 = {}
if sys.version_info[0] >= 3:
encoding_py3['encoding'] = 'utf-8'

try:
invocation = ["clang-format", src_file_name,
"-i", f'--style=file:{os.path.dirname(__file__)}/../.clang-format']

proc = subprocess.Popen(
invocation,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
**encoding_py3)
except OSError as _:
print("error.")
assert (False)
proc_stdout = proc.stdout
proc_stderr = proc.stderr
outs = list(proc_stdout.readlines())
errs = list(proc_stderr.readlines())
proc.wait()

if proc.returncode:
print("error.", subprocess.list2cmdline(
invocation), proc.returncode, errs)
assert (False)
Loading