forked from GetAnsa/rascalWrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseWithRascal
More file actions
executable file
·90 lines (66 loc) · 2.32 KB
/
parseWithRascal
File metadata and controls
executable file
·90 lines (66 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
if [[ $# != 2 ]]
then
echo "Usage: parseWithRascal <grammar-file> <absolute-path-to-parse-target>"
exit 1
fi
execDir="$(dirname $0)"
source $execDir/commonPrefix
# similar to the standard rascal command in this repo but with some flavor
# to start we'll keep it mostly proof of concept
# could stand some refactoring to limit copy pasta
fileFound=""
if [[ -f $1 ]]
then
fileFound="$1"
fi
if [[ -f "${1}.rsc" ]]
then
fileFound="${1}.rsc"
fi
if [[ -z "$fileFound" ]]
then
echo "Grammar specified is not a valid rascal file"
exit 2
fi
echo "fileFound is $fileFound"
#module=`head -n 1 $fileFound | sed 's/module *//'`
module=`sed -n 's/^module *//p' $fileFound | head -n 1`
if [[ -z $module ]]
then
echo "no module declared in source file"
exit 1
fi
echo "module is $module"
moduleDirFragment=`echo "$module" | sed 's/\\\\//g' | sed 's|::|/|g'`
echo "moduleDirFragment is $moduleDirFragment"
# find top level syntax nonterminal
start=`cat $fileFound | awk '/^\s*start syntax/ {print $3}'`
echo "syntax start is $start"
tmpDir=`mktemp -d`
mkdir $tmpDir/META-INF
echo "Project-Name: convenientRascal" > $tmpDir/META-INF/RASCAL.MF
echo "Source: src/$moduleDirFragment/rascal" >> $tmpDir/META-INF/RASCAL.MF
mkdir -p $tmpDir/src/$moduleDirFragment/rascal
cp $fileFound $tmpDir/src/$moduleDirFragment/rascal/
echo "module Parse" > $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo "import ${module};" >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo "import IO;" >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo "import ParseTree;" >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo 'int main(list[str] args) {' >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo " println(parse(#start[$start], |file://$2|));" >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo ' return 0;' >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
echo '}' >> $tmpDir/src/$moduleDirFragment/rascal/Parse.rsc
# debug prints
echo "file we're parsing is $2"
echo "contents of tmpDir:"
find $tmpDir -type f | while read line
do
echo $line
cat $line
done
# end debug
cd $tmpDir
/opt/homebrew/opt/openjdk/bin/java -jar ~/.rascal/rascal-*.jar Parse.rsc ${@:3}