From cdb94436352c83ed8416cd4215b32d350221dc6f Mon Sep 17 00:00:00 2001 From: marsninja Date: Sun, 17 Sep 2023 11:48:02 -0400 Subject: [PATCH] Made module level docstrings not required --- examples/manual_code/circle.jac | 5 ++--- examples/manual_code/test.jac | 2 ++ jaclang/jac/lexer.py | 22 ++++++++++++++++++++++ jaclang/jac/parser.py | 4 +++- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 examples/manual_code/test.jac diff --git a/examples/manual_code/circle.jac b/examples/manual_code/circle.jac index ef0421731..9db482df7 100644 --- a/examples/manual_code/circle.jac +++ b/examples/manual_code/circle.jac @@ -1,5 +1,4 @@ """ -(Module docstring are Required in a valid Jac) This module demonstrates a simple circle class and a function to calculate the area of a circle. """ import:py math; @@ -54,8 +53,8 @@ with entry {c = Circle(RAD);} # Global also works here with entry:__main__ { # TODO: add name == option abstract feature # To run the program functionality - print(f"Area of a circle with radius 5: {calculate_area(RAD)}"); - print(f"Area of a {c.shape_type.value} with radius 5: {c.area()}"); + print(f"Area of a circle with radius {RAD} using function: {calculate_area(RAD)}"); + print(f"Area of a {c.shape_type.value} with radius {RAD} using class: {c.area()}"); } diff --git a/examples/manual_code/test.jac b/examples/manual_code/test.jac new file mode 100644 index 000000000..cb7b6ba31 --- /dev/null +++ b/examples/manual_code/test.jac @@ -0,0 +1,2 @@ +"""This is a docstring""" +with entry { print("hello"); } diff --git a/jaclang/jac/lexer.py b/jaclang/jac/lexer.py index 9a3b5bd07..e35bd3d9e 100644 --- a/jaclang/jac/lexer.py +++ b/jaclang/jac/lexer.py @@ -14,8 +14,10 @@ def __init__( input_ir: str, base_path: str = "", prior: Transform | None = None, + fstr_override: bool = False, ) -> None: """Initialize lexer.""" + self.fstr_override = fstr_override Transform.__init__(self, mod_path, input_ir, base_path, prior) # type: ignore self.ir: Generator = self.ir @@ -376,6 +378,26 @@ def transform(self, ir: str) -> Generator: """Tokenize the input.""" return self.tokenize(ir) + def tokenize(self, text: str) -> Generator: + """Tokenize override for no module level docstring.""" + has_doc_string_start = False + for tok in super().tokenize(text): + if ( + tok.type != "DOC_STRING" + and not has_doc_string_start + and not self.fstr_override + ): + dtok = Token() + dtok.type = "DOC_STRING" + dtok.value = '""""""' + dtok.lineno = 1 + dtok.lineidx = 0 + dtok.index = 0 + dtok.end = 0 + yield dtok + has_doc_string_start = True + yield tok + def error(self, t: Token) -> None: """Raise an error for illegal characters.""" self.cur_line = self.lineno diff --git a/jaclang/jac/parser.py b/jaclang/jac/parser.py index a50160a83..9905c16b4 100644 --- a/jaclang/jac/parser.py +++ b/jaclang/jac/parser.py @@ -1437,7 +1437,9 @@ def find_and_concat_fstr_pieces(tup: tuple) -> str: tree = JacParserExpr( mod_path="", input_ir=JacLexer( - mod_path="", input_ir=find_and_concat_fstr_pieces(tree) + mod_path="", + input_ir=find_and_concat_fstr_pieces(tree), + fstr_override=True, ).ir, ).ir_tup[2] kids = tree[2:]