From 7c6a3168cb1d8e42409c14f3a6ebfd0080e6dd61 Mon Sep 17 00:00:00 2001 From: Stoney Jackson Date: Thu, 18 Apr 2024 01:54:32 +0000 Subject: [PATCH] refactor: move specs into separate files --- src/langs/__init__.py | 0 src/langs/java.py | 65 +++++++++++++++++ src/langs/python.py | 40 ++++++++++ src/plcc.py | 166 +++++------------------------------------- 4 files changed, 125 insertions(+), 146 deletions(-) create mode 100644 src/langs/__init__.py create mode 100644 src/langs/java.py create mode 100644 src/langs/python.py diff --git a/src/langs/__init__.py b/src/langs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/langs/java.py b/src/langs/java.py new file mode 100644 index 00000000..d5f3f95c --- /dev/null +++ b/src/langs/java.py @@ -0,0 +1,65 @@ +spec = { + "abstractStubFormatString" : """\ +//{base}:top// +//{base}:import// +import java.util.*; + +public abstract class {base}{ext} /*{base}:class*/ {{ + + public static final String $className = "{base}"; + public static {base} parse(Scan scn$, Trace trace$) {{ + Token t$ = scn$.cur(); + Token.Match match$ = t$.match; + switch(match$) {{ +{cases} + default: + throw new PLCCException( + "Parse error", + "{base} cannot begin with " + t$.errString() + ); + }} + }} + +//{base}// +}} +""", + + "stubFormatString" : """\ +//{cls}:top// +//{cls}:import// +import java.util.*; + +// {ruleString} +public class {cls}{ext} /*{cls}:class*/ {{ + + public static final String $className = "{cls}"; + public static final String $ruleString = + "{ruleString}"; + +{decls} + + public {cls}({params}) {{ +//{cls}:init// +{inits} + }} + + public static {cls} parse(Scan scn$, Trace trace$) {{ + if (trace$ != null) + trace$ = trace$.nonterm("{lhs}", scn$.lno); +{parse} + }} + +//{cls}// +}} +""", + "extendFormatString" : ' extends {cls}', + "declFormatString" : 'public {fieldType} {field};', + "initFormatString" : 'this.{field} = {field};', + "paramFormatString" : '{fieldType} {field}', + "semFlag" : 'semantics', + "lineComment" : '//', + "blockCommentStart" : "/*", + "blockCommentEnd" : "*/", + "destFlag" : 'destdir', + "fileExt" : '.java' +} diff --git a/src/langs/python.py b/src/langs/python.py new file mode 100644 index 00000000..28d913d3 --- /dev/null +++ b/src/langs/python.py @@ -0,0 +1,40 @@ +spec = { + "abstractStubFormatString" : """\ +#{base}:top# +#{base}:import# + +class {base}({ext}): #{base}:class# + + className = "{base}" + + #{base}# +""", + + "stubFormatString" : """\ +#{cls}:top# +#{cls}:import# + +# {ruleString} +class {cls}({ext}): #{cls}:class# + + className = "{cls}" + ruleString = "{ruleString}" +{decls} + + def __init__({params}): + #{cls}:init# +{inits} + +#{cls}# +""", + "extendFormatString" : '{cls}', + "declFormatString" : '{field} = None', + "initFormatString" : 'self.{field} = {field}', + "paramFormatString" : '{field}', + "semFlag" : 'python_semantics', + "lineComment" : '#', + "blockCommentStart" : "'''", + "blockCommentEnd" : "'''", + "destFlag" : 'python_destdir', + "fileExt" : '.py' +} diff --git a/src/plcc.py b/src/plcc.py index e631291f..d2f3c896 100644 --- a/src/plcc.py +++ b/src/plcc.py @@ -24,8 +24,13 @@ import io import shutil import tempfile + +from langs.java import spec as java_spec +from langs.python import spec as python_spec + argv = sys.argv[1:] # skip over the command-line argument + # current file information Fname = '' # current file name (STDIN if standard input) Lno = 0 # current line number in file @@ -112,17 +117,8 @@ def main(): nxt = nextLine() # nxt is the next line generator lex(nxt) # lexical analyzer generation par(nxt) # LL(1) check and parser generation - sem(nxt, stubs) # java semantic actions - - # python semantic actions - sem(nxt, python_stubs, - semFlag='python_semantics', - lineComment='#', - blockCommentStart="'''", blockCommentEnd="'''", - destFlag='python_destdir', - ext='.py' - ) - + sem(nxt, stubs, **java_spec) + sem(nxt, python_stubs, **python_spec) done() def plccInit(): @@ -345,131 +341,6 @@ def par(nxt): parFinishUp() -javaSpec = { - "abstractStubFormatString" : """\ -//{base}:top// -//{base}:import// -import java.util.*; - -public abstract class {base}{ext} /*{base}:class*/ {{ - - public static final String $className = "{base}"; - public static {base} parse(Scan scn$, Trace trace$) {{ - Token t$ = scn$.cur(); - Token.Match match$ = t$.match; - switch(match$) {{ -{cases} - default: - throw new PLCCException( - "Parse error", - "{base} cannot begin with " + t$.errString() - ); - }} - }} - -//{base}// -}} -""", - - "stubFormatString" : """\ -//{cls}:top// -//{cls}:import// -import java.util.*; - -// {ruleString} -public class {cls}{ext} /*{cls}:class*/ {{ - - public static final String $className = "{cls}"; - public static final String $ruleString = - "{ruleString}"; - -{decls} - - public {cls}({params}) {{ -//{cls}:init// -{inits} - }} - - public static {cls} parse(Scan scn$, Trace trace$) {{ - if (trace$ != null) - trace$ = trace$.nonterm("{lhs}", scn$.lno); -{parse} - }} - -//{cls}// -}} -""", - "extendFormatString" : ' extends {cls}', - "declFormatString" : 'public {fieldType} {field};', - "initFormatString" : 'this.{field} = {field};', - "paramFormatString" : '{fieldType} {field}' -} - -pythonSpec = { - "abstractStubFormatString" : """\ -#{base}:top# -#{base}:import# - -class {base}({ext}): #{base}:class# - - className = "{base}" - - #{base}# -""", - - "stubFormatString" : """\ -#{cls}:top# -#{cls}:import# - -# {ruleString} -class {cls}({ext}): #{cls}:class# - - className = "{cls}" - ruleString = "{ruleString}" -{decls} - - def __init__({params}): - #{cls}:init# -{inits} - -#{cls}# -""", - "extendFormatString" : '{cls}', - "declFormatString" : '{field} = None', - "initFormatString" : 'self.{field} = {field}', - "paramFormatString" : '{field}' -} - -python_abstractStubFormatString = """\ -#{base}:top# -#{base}:import# - -class {base}({ext}): #{base}:class# - - className = "{base}" - - #{base}# -""" - -python_stubFormatString = """\ -#{cls}:top# -#{cls}:import# - -# {ruleString} -class {cls}({ext}): #{cls}:class# - - className = "{cls}" - ruleString = "{ruleString}" -{decls} - - def __init__({params}): - #{cls}:init# -{inits} - -#{cls}# -""" - - def parFinishUp(): global STDP, startSymbol, nonterms, extends, derives, rules if not rules: @@ -531,8 +402,8 @@ def parFinishUp(): death('Failure copying {} from {} to {}'.format(fname, std, dst)) # build parser stub classes - buildStubs(stubs, **javaSpec) - buildStubs(python_stubs, **pythonSpec) + buildStubs(stubs, **java_spec) + buildStubs(python_stubs, **python_spec) # build the _Start.java file from the start symbol buildStart() @@ -760,7 +631,8 @@ def buildStubs( extendFormatString, declFormatString, initFormatString, - paramFormatString): + paramFormatString, + **ignored_kwargs): global fields, derives for cls in derives: # make parser stubs for all abstract classes @@ -1039,15 +911,17 @@ def semFinishUp(stubs, destFlag='destdir', ext='.java'): print(f' {cls}{ext}') def sem(nxt, stubs, - semFlag='semantics', - lineComment='//', - blockCommentStart="/*", blockCommentEnd="*/", - destFlag='destdir', - ext='.java'): + semFlag, + lineComment, + blockCommentStart, + blockCommentEnd, + destFlag, + fileExt, + **ignored_kwargs): global argv # print('=== semantic routines') if not getFlag(semFlag): - semFinishUp(stubs, destFlag, ext) + semFinishUp(stubs, destFlag, fileExt) done() for line in nxt: line = line.strip() @@ -1106,7 +980,7 @@ def sem(nxt, stubs, if mod: deathLNO(f'no stub for class {cls} -- cannot replace {lineComment}{cls}:{mod}{lineComment}') stubs[cls] = codeString - semFinishUp(stubs, destFlag, ext) + semFinishUp(stubs, destFlag, fileExt) def getCode(nxt):