Skip to content

Commit

Permalink
refactor: move specs into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneyJackson committed Apr 18, 2024
1 parent 256dcb1 commit 7c6a316
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 146 deletions.
Empty file added src/langs/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions src/langs/java.py
Original file line number Diff line number Diff line change
@@ -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'
}
40 changes: 40 additions & 0 deletions src/langs/python.py
Original file line number Diff line number Diff line change
@@ -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'
}
166 changes: 20 additions & 146 deletions src/plcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7c6a316

Please sign in to comment.