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

53 add option to parsejava to print ast in json #68

Merged
merged 39 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0734e66
feat: add base jackson files to lib folder
Rarity-Belle Oct 11, 2023
00e878e
fix: move lib into src
Rarity-Belle Oct 16, 2023
8d8b5cf
Merge branch 'main' into 53-add-option-to-parsejava-to-print-ast-in-json
Rarity-Belle Oct 18, 2023
fa122d2
feat: add ParseJSONAST and test grammar
Rarity-Belle Oct 18, 2023
caf1594
update java version (revert me later?)
StoneyJackson Oct 18, 2023
94213cb
update java (part 2)
StoneyJackson Oct 18, 2023
f6a67ef
fix: use raw strings for regex in plcc.py
StoneyJackson Oct 18, 2023
2bcabca
permission test
Rarity-Belle Oct 20, 2023
299b2cf
Revert "permission test"
Rarity-Belle Oct 20, 2023
390d6ca
fix: update ParseJSONAST
WilliamBowery Oct 23, 2023
5c52d8e
feat: update parse script to include json option
WilliamBowery Oct 24, 2023
21bad33
feat: convert the parse tree to json format
WilliamBowery Oct 24, 2023
1542328
feat: add compiler command for jar files
WilliamBowery Oct 24, 2023
4395892
fix: update name of ParseJSONAST
WilliamBowery Oct 24, 2023
4d8fb68
fix: fixed grep eating --json
Rarity-Belle Oct 24, 2023
32adb59
fix: fixed missing bracket
Rarity-Belle Oct 24, 2023
4392582
fix: fix parse brackets
Rarity-Belle Oct 24, 2023
fcbb81f
make it compile, run, and explode
StoneyJackson Oct 25, 2023
ad0e317
fix: serializer serializes now
Rarity-Belle Nov 1, 2023
b96260c
fix: show parent class names & indent output
Rarity-Belle Nov 1, 2023
3a2e60f
fix: remove use of new Java switch statements/expressions (#62)
StoneyJackson Oct 29, 2023
d0bc717
wip: show class names in generated JSON file
Rarity-Belle Nov 1, 2023
302f7cd
wip: add python option to plccmk
Rarity-Belle Nov 9, 2023
7c8eeba
wip: add --python option to plcc.py
Rarity-Belle Nov 15, 2023
07e545b
fix: incorrect array index in --python option
Rarity-Belle Nov 15, 2023
22b7f8d
test: prints-json-ast
StoneyJackson Nov 16, 2023
f7e7837
test: fix last commit
StoneyJackson Nov 16, 2023
38ec7d8
fix: update python option to json-ast
Rarity-Belle Nov 16, 2023
2856e54
fix: fix malformed flag, temp fix?
Rarity-Belle Nov 16, 2023
fd9e674
fix make --jsonast option copy ParseJsonAst.java to /Java
WilliamBowery Nov 16, 2023
f9790fc
fix: update --jsonast to --json_ast
WilliamBowery Nov 16, 2023
08dc5e8
fix: rename output file, fix(?) bats test
Rarity-Belle Nov 16, 2023
b4c1405
feat: ParseJsonAST prints to stdout
Rarity-Belle Nov 17, 2023
2399774
wip: test works as intended?
Rarity-Belle Nov 17, 2023
67b52f7
cleanup: remove example grammar, program, and json files from root
Rarity-Belle Nov 17, 2023
732a302
Merge branch 'main' into 53-add-option-to-parsejava-to-print-ast-in-json
StoneyJackson Nov 21, 2023
dc1f2ed
Merge branch 'main' into 53-add-option-to-parsejava-to-print-ast-in-json
Rarity-Belle Nov 29, 2023
5fa2725
docs: add info relating to --json_ast option
Rarity-Belle Nov 29, 2023
8c20da9
tweaks
StoneyJackson Nov 29, 2023
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Java/
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
Expand All @@ -43,7 +42,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down
2 changes: 2 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ tasks:
export PATH="$LIBPLCC:$PATH"
' >> "$HOME/.bashrc"
source "$HOME/.bashrc"
- name: Update Java
command: sdk install java < /dev/null
1 change: 1 addition & 0 deletions AST.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"exp":{"prim":{},"rands":{"expList":[{"lit":{"match":"LIT","str":"5","lno":1,"line":"+(5, 1)\n","eof":false}},{"lit":{"match":"LIT","str":"1","lno":1,"line":"+(5, 1)\n","eof":false}}]}}}
35 changes: 35 additions & 0 deletions ASTroot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Program" : {
"$type" : "Program",
"exp" : {
"$type" : "PrimappExp",
"prim" : {
"$type" : "AddPrim"
},
"rands" : {
"$type" : "Rands",
"expList" : [ "java.util.ArrayList", [ {
"$type" : "LitExp",
"lit" : {
"$type" : "Token",
"match" : "LIT",
"str" : "3",
"lno" : 1,
"line" : "+(3, 2)\n",
"eof" : false
}
}, {
"$type" : "LitExp",
"lit" : {
"$type" : "Token",
"match" : "LIT",
"str" : "2",
"lno" : 1,
"line" : "+(3, 2)\n",
"eof" : false
}
} ] ]
}
}
}
}
21 changes: 21 additions & 0 deletions grammar
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
skip WHITESPACE '\s+'
skip COMMENT '%.*'
LIT '\d+'
LPAREN '\('
RPAREN '\)'
COMMA ','
ADDOP '\+'
SUBOP '\-'
ADD1OP 'add1'
SUB1OP 'sub1'
VAR '[A-Za-z]\w*'
%
<program> ::= <exp>
<exp>:LitExp ::= <LIT>
<exp>:VarExp ::= <VAR>
<exp>:PrimappExp ::= <prim> LPAREN <rands> RPAREN
<rands> **= <exp> +COMMA
<prim>:AddPrim ::= ADDOP
<prim>:SubPrim ::= SUBOP
<prim>:Add1Prim ::= ADD1OP
<prim>:Sub1Prim ::= SUB1OP
40 changes: 40 additions & 0 deletions src/Std/ParseJsonAst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.*;
import java.io.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;



public class ParseJsonAst extends ProcessFiles {

// Parse the program and call $ok() on the resulting parse tree
public void action(Scan scn, Trace trace) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator
.builder()
.allowIfBaseType(Object.class) // Can change "Object" to any type, we can look back at this later to be more specific
.build();

objectMapper.activateDefaultTypingAsProperty(ptv, ObjectMapper.DefaultTyping.NON_FINAL, "$type");

_Start parseTree = _Start.parse(scn, trace);
parseTree.$ok();
try {
objectMapper.writeValue(new File("ASTroot.json"), parseTree);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should print the AST to stdout. Can we do that?

} catch (IOException e) {
e.printStackTrace();
}
}

// Read programs from command-line files
// and then read programs from standard input.
public static void main(String [] args) {
new ParseJsonAst().processFiles(args);
}
}
12 changes: 9 additions & 3 deletions src/Std/Token.pattern
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ public class Token {
}
String what = "??";
switch(match.tokType) {
case SKIP -> what = "skip";
case TOKEN -> what = "token";
case LINE_TOGGLE -> what = "token (line toggle)";
case SKIP:
what = "skip";
break;
case TOKEN:
what = "token";
break;
case LINE_TOGGLE:
what = "token (line toggle)";
break;
}
System.out.println(
String.format("%s %s '%s'",what,match.toString(),match.pattern)
Expand Down
Binary file added src/lib/jackson-annotations-2.15.2.jar
Binary file not shown.
Binary file added src/lib/jackson-core-2.15.2.jar
Binary file not shown.
Binary file added src/lib/jackson-databind-2.15.2.jar
Binary file not shown.
27 changes: 26 additions & 1 deletion src/parse
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/bash

JACKSON_VERSION="2.15.2"
JACKSON_ANNOTATIONS="${LIBPLCC}/lib/jackson-annotations-${JACKSON_VERSION}.jar"
JACKSON_CORE="${LIBPLCC}/lib/jackson-core-${JACKSON_VERSION}.jar"
JACKSON_DATABIND="${LIBPLCC}/lib/jackson-databind-${JACKSON_VERSION}.jar"
CP="${JACKSON_ANNOTATIONS}:${JACKSON_CORE}:${JACKSON_DATABIND}"

[ -d Java ] || {
echo "Java directory missing" >&2
exit 1
Expand All @@ -10,4 +16,23 @@
exit 2
}

java -cp ./Java Parse $*
if echo "$*" | grep -- "--json" &> /dev/null
then

ARGS=()
while [ $# -gt 0 ] ; do
case "$1" in
--json)
shift
;;
*)
ARGS+=("$1")
shift
;;
esac
done

java -cp "./Java:${CP}" ParseJsonAst "${ARGS[@]}"
else
java -cp ./Java Parse $*
fi
21 changes: 12 additions & 9 deletions src/plcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io
import shutil
import tempfile

argv = sys.argv[1:] # skip over the command-line argument

# current file information
Expand Down Expand Up @@ -113,9 +112,13 @@ def main():
sem(nxt) # semantic actions

def plccInit():
global flags, STD, STDT, STDP
global flags, argv, STD, STDT, STDP
STDT = ['ILazy','IMatch','IScan','ITrace', 'Trace', 'PLCCException', 'Scan']
STDP = ['ProcessFiles','Parse','Rep']
print(argv)
if len(argv) > 1:
if argv[0] == "--python":
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plcc.py has a mechanism for handling command-line arguments. Unless there is a good reason, we should probably work within that framework.

Specifically, there is a loop in main() that walks through all the command line arguments and calls processFlag() to populate a global dict with flags. The rest of the system interrogates flags to decide what to do.

STDP.append('ParseJsonAst')
STD = STDT + STDP
STD.append('Token')
# file-related flags -- can be overwritten
Expand All @@ -140,7 +143,7 @@ def lex(nxt):
# turn off when all flags have been processed
flagSwitch = True # turn off after all the flags have been processed
for line in nxt:
line = re.sub('\s+#.*', '', line) # remove trailing comments ...
line = re.sub(r'\s+#.*', '', line) # remove trailing comments ...
# NOTE: a token that has a substring like this ' #' will mistakenly be
# considered as a comment. Use '[ ]#' instead
line = line.strip()
Expand Down Expand Up @@ -174,7 +177,7 @@ def qsub(match):
jpat = match.group(1)
# print('>>> match found: jpat={}'.format(jpat))
return ''
pat = "\s'(.*)'$"
pat = r"\s'(.*)'$"
# print('>>> q1 pat={}'.format(pat))
line = re.sub(pat, qsub, line)
if jpat:
Expand All @@ -184,7 +187,7 @@ def qsub(match):
# print('>>> q1 match found: line={} jpat={}'.format(line,jpat))
pass
else:
pat = '\s"(.*)"$'
pat = r'\s"(.*)"$'
# print('>>> q2 pat={}'.format(pat))
line = re.sub(pat, qsub, line)
if jpat:
Expand Down Expand Up @@ -1208,19 +1211,19 @@ def defangRHS(item):
return (tnt, field)

def isID(item):
return re.match('[a-z]\w*#?$', item)
return re.match(r'[a-z]\w*#?$', item)

def isNonterm(nt):
debug('[isNonterm] nt={}'.format(nt))
if nt == 'void' or len(nt) == 0:
return False
return re.match('[a-z]\w*#?$', nt)
return re.match(r'[a-z]\w*#?$', nt)

def isClass(cls):
return re.match('[A-Z][\$\w]*$', cls) or cls == 'void'
return re.match(r'[A-Z][\$\w]*$', cls) or cls == 'void'

def isTerm(term):
return re.match('[A-Z][A-Z\d_$]*$', term) or term == '$LINE'
return re.match(r'[A-Z][A-Z\d_$]*$', term) or term == '$LINE'

def nt2cls(nt):
# return the class name of the nonterminal nt
Expand Down
27 changes: 26 additions & 1 deletion src/plccmk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ LIB="${LIBPLCC:-/usr/local/pub/plcc/PLCC}"
PYTHON3=python3
PLCC="$LIB/plcc.py"

JACKSON_VERSION="2.15.2"
JACKSON_ANNOTATIONS="${LIBPLCC}/lib/jackson-annotations-${JACKSON_VERSION}.jar"
JACKSON_CORE="${LIBPLCC}/lib/jackson-core-${JACKSON_VERSION}.jar"
JACKSON_DATABIND="${LIBPLCC}/lib/jackson-databind-${JACKSON_VERSION}.jar"
CP="${JACKSON_ANNOTATIONS}:${JACKSON_CORE}:${JACKSON_DATABIND}"

if [ "$1" = "-c" ];
then
rm Java/*.java Java/*.class &>/dev/null
Expand Down Expand Up @@ -33,5 +39,24 @@ $PYTHON3 "$PLCC" $FILES || {
echo "Java directory missing" >&2
exit 5
}
if echo "$*" | grep -- "--python" &> /dev/null
then

ARGS=()
while [ $# -gt 0 ] ; do
case "$1" in
--python)
shift
;;
*)
ARGS+=("$1")
shift
;;
esac
done

(cd ./Java ; javac -cp ".:${CP}" *.java)
else
(cd ./Java ; javac *.java)
fi

(cd ./Java ; javac *.java)
1 change: 1 addition & 0 deletions test.v0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+(3, 2)