-
Notifications
You must be signed in to change notification settings - Fork 6
/
parser.ts
79 lines (67 loc) 路 2.61 KB
/
parser.ts
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
import { parse } from 'java-ast'
import { ParseTree } from 'antlr4ts/tree/ParseTree'
import * as diagnostics from './diagnostics';
import { TextDocument } from 'vscode-languageserver';
import * as pStandards from './grammer/terms/preprocessingsnippets'
import * as log from './scripts/syslogs'
const childProcess = require('child_process');
// Tuple -> current Node, Parent Node
export let tokenArray: [ParseTree, ParseTree][] = new Array();
let _tokenCounter = -1
// Tuple -> Current Node, Parent Node
// Helps in traversing up the tree
export let wholeAST: [ParseTree, ParseTree | undefined][] = new Array();
let _wholeCounter = -1
// Currently constructed AST after the last character change
export let ast: any
export function parseAST(processedText: string, textDocument: TextDocument) {
ast = parse(processedText)
tokenArray = []
_tokenCounter = -1
wholeAST = []
_wholeCounter = -1
for(let i = 0; i < ast.childCount; i++){
extractTokens(ast.children![i])
}
for(let i = 0; i < ast.childCount; i++){
wholeASTExtract(ast.children![i])
}
console.log("Break point here to obtain AST")
// mkdir /out/compile
// make sure to set .classpath for Processing core as environment variable
// This suites for raw java case - should handle for default and setupDraw case
try{
childProcess.execSync(`echo \"${processedText}\" > ${__dirname}/compile/${pStandards.defaultClassName}.java`)
log.writeLog(`Java File creation successful`)
} catch(e) {
log.writeLog(`[[ERR]] - Error in Java File Creation`)
}
try{
childProcess.execSync(`javac -classpath ${__dirname.substring(0,__dirname.length-11)}/pcore/ ${__dirname}/compile/${pStandards.defaultClassName}.java -Xlint:none -Xstdout ${__dirname}/compile/error.txt`,
{ stdio:[ 'inherit', 'pipe', 'pipe' ] })
log.writeLog(`Java File compilation successful`)
} catch(e) {
log.writeLog(`[[ERR]] - Error in Java File Compilation`)
}
// Wrote methods to handle Error in the Error Stream
// diagnostics.cookDiagnosticsReport(processedText)
diagnostics.cookCompilationDiagnostics(processedText, `${__dirname}/compile/${pStandards.defaultClassName}.java`)
log.writeLog("Parse Tree construction Successfully")
}
function extractTokens(gotOne: ParseTree){
for(let j = 0; j < gotOne.childCount; j++){
if(gotOne.getChild(j).childCount == 0){
_tokenCounter +=1
tokenArray[_tokenCounter] = [gotOne.getChild(j),gotOne]
}
extractTokens(gotOne.getChild(j))
}
}
// top -> bottom ; left -> right
function wholeASTExtract(gotOne: ParseTree){
_wholeCounter += 1
wholeAST[_wholeCounter] = [gotOne,gotOne.parent]
for(let j=0;j<gotOne.childCount;j++){
wholeASTExtract(gotOne.getChild(j))
}
}