-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathantlr4nim.nimble
78 lines (63 loc) · 2.24 KB
/
antlr4nim.nimble
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
# Package
version = "0.1.0"
author = "jan0sc"
description = "Nim interface to ANTLR4 listener/visitor via jsffi"
license = "MIT"
srcDir = "src"
# Dependencies
requires "nim >= 1.4.2"
import strformat
import os
# Tasks
task prepare, "Convert ANTLR4 .js modules to .mjs for use with antlr4nim.":
var n = paramcount()
var grammar = paramStr(n)
var allOK = true
# Lexer
if fileExists(fmt"{grammar}Lexer.js"):
if fileExists(fmt"{grammar}Lexer.mjs"):
echo fmt"ERROR: {grammar}Lexer.mjs is already present."
allOK = false
else:
exec fmt"cp {grammar}Lexer.js {grammar}Lexer.mjs"
echo fmt"Copied {grammar}Lexer.js to {grammar}Lexer.mjs"
else:
echo fmt"ERROR: {grammar}Lexer.js not found."
allOK = false
#Parser
if fileExists(fmt"{grammar}Parser.js"):
if fileExists(fmt"{grammar}Parser.mjs"):
echo fmt"ERROR: {grammar}Parser.mjs is already present."
allOK = false
else:
exec fmt"cat {grammar}Parser.js | sed 's/{grammar}Listener.js/{grammar}Listener.mjs/' | sed 's/{grammar}Visitor.js/{grammar}Visitor.mjs/' > {grammar}Parser.mjs"
echo fmt"Copied {grammar}Parser.js to {grammar}Parser.mjs and updated imports"
else:
echo fmt"ERROR: {grammar}Parser.js not found."
allOK = false
# Listener and Visitor
var foundOne = false
if fileExists(fmt"{grammar}Listener.js"):
foundOne = true
if fileExists(fmt"{grammar}Listener.mjs"):
echo fmt"ERROR: {grammar}Listener.mjs is already present."
allOK = false
else:
exec fmt"cp {grammar}Listener.js {grammar}Listener.mjs"
echo fmt"Copied {grammar}Listener.js to {grammar}Listener.mjs"
if fileExists(fmt"{grammar}Visitor.js"):
foundOne = true
if fileExists(fmt"{grammar}Visitor.mjs"):
echo fmt"ERROR: {grammar}Visitor.mjs is already present."
allOK = false
else:
exec fmt"cp {grammar}Visitor.js {grammar}Visitor.mjs"
echo fmt"Copied {grammar}Visitor.js to {grammar}Visitor.mjs"
if not foundOne:
echo fmt"ERROR: Neither {grammar}Listener.js nor {grammar}Visitor.js found."
allOK = false
# Success?
if allOK:
echo "OK: ANTLR4 files are now ready for antlr4nim."
else:
echo "FAIL: Some ANTLR4 files could not be processed."