Skip to content

Commit 5631b33

Browse files
Add script for generating grammar
1 parent 2bf6bcd commit 5631b33

File tree

11 files changed

+4317
-6159
lines changed

11 files changed

+4317
-6159
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "Grammar/Builder"]
2+
path = Grammar/Builder
3+
url = https://github.com/SwiftTools/ANTLR-Swift-Target-Builder.git

Cartfile.resolved

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
github "SwiftTools/Antlr-Swift-Runtime" "v0.0.1"
2-
github "Quick/Nimble" "v3.2.0"
3-
github "Quick/Quick" "v0.9.1"
1+
github "SwiftTools/Antlr-Swift-Runtime" "v4.5.1.3"
2+
github "Quick/Nimble" "v4.0.0"
3+
github "Quick/Quick" "v0.9.2"

Grammar/Builder

Submodule Builder added at f1cff27

Swift.g4 renamed to Grammar/Swift.g4

+2-2
Original file line numberDiff line numberDiff line change
@@ -1025,8 +1025,8 @@ prefix_operator : {SwiftSupport.isPrefixOp(_input)}? swift_operator ;
10251025
postfix_operator : {SwiftSupport.isPostfixOp(_input)}? swift_operator ;
10261026

10271027
swift_operator
1028-
: operator_head ({_input.get(_input.index()-1).getType()!=WS}? operator_character)*
1029-
| dot_operator_head ({_input.get(_input.index()-1).getType()!=WS}? dot_operator_character)*
1028+
: operator_head ({try _input.get(_input.index() - 1).getType() != SwiftParser.WS}? operator_character)*
1029+
| dot_operator_head ({try _input.get(_input.index() - 1).getType() != SwiftParser.WS}? dot_operator_character)*
10301030
;
10311031

10321032
operator_character

Grammar/generate.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Setup
4+
5+
pushd . > /dev/null
6+
cd `dirname $0`
7+
scriptRoot=`pwd`
8+
popd > /dev/null
9+
10+
# Paths
11+
12+
builder="$scriptRoot/Builder/build.sh"
13+
grammarName="Swift.g4"
14+
grammarFolder="$scriptRoot"
15+
bin=~/.antlrswift/bin
16+
src=~/.antlrswift/src
17+
gen=~/.antlrswift/AntlrGenerated
18+
target="$scriptRoot/../SwiftGrammar/Sources/Swift/"
19+
20+
# Functions
21+
22+
selectJar() {
23+
for i in `ls "$bin" | grep "jar" | grep -v "\-tests" | grep -v "original\-"`
24+
do
25+
echo $i
26+
return 0
27+
done
28+
29+
return 1
30+
}
31+
32+
# Build
33+
34+
selectJar || "$builder" --gitFolder "$src" --installFolder "$bin"
35+
36+
cp "$grammar" "$bin"
37+
38+
jar="$bin/$(selectJar)"
39+
40+
mkdir -p "$gen"
41+
42+
pushd . > /dev/null
43+
44+
cd "$grammarFolder"
45+
java -cp "$jar" org.antlr.v4.Tool -Dlanguage=Swift -o "$gen" "$grammarName"
46+
47+
popd
48+
49+
find "$gen" -type f ! -iname "*.swift" -delete
50+
51+
patch "$gen/SwiftBaseListener.swift" "$scriptRoot/patch.diff"
52+
53+
cp -R "$gen" "$target"

Grammar/patch.diff

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- SwiftGrammar/Sources/Swift/AntlrGenerated/SwiftBaseListener2.swift 2016-04-10 22:33:10.000000000 +0300
2+
+++ SwiftGrammar/Sources/Swift/AntlrGenerated/SwiftBaseListener.swift 2016-04-10 22:33:33.000000000 +0300
3+
@@ -9,6 +9,9 @@
4+
* of the available methods.
5+
*/
6+
public class SwiftBaseListener: SwiftListener {
7+
+ public init() {
8+
+ }
9+
+
10+
/**
11+
* {@inheritDoc}
12+
*

SwiftGrammar/Sources/Swift/AntlrGenerated/SwiftLexer.swift

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import Antlr4
44
public class SwiftLexer: Lexer {
55
internal static var _decisionToDFA: [DFA] = {
66
var decisionToDFA = [DFA]()
7-
for i in 0..<SwiftLexer._ATN.getNumberOfDecisions() {
7+
let length = SwiftLexer._ATN.getNumberOfDecisions()
8+
for i in 0..<length {
89
decisionToDFA.append(DFA(SwiftLexer._ATN.getDecisionState(i)!, i))
910
}
1011
return decisionToDFA
1112
}()
1213

13-
internal let _sharedContextCache:PredictionContextCache = PredictionContextCache()
14+
internal static let _sharedContextCache:PredictionContextCache = PredictionContextCache()
1415
public static let T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7,
1516
T__7=8, T__8=9, T__9=10, T__10=11, T__11=12, T__12=13,
1617
T__13=14, T__14=15, T__15=16, T__16=17, T__17=18, T__18=19,
@@ -120,18 +121,17 @@ public class SwiftLexer: Lexer {
120121
*/
121122
//@Deprecated
122123
public let tokenNames: [String?]? = {
123-
var tokenNames = [String?]()
124-
125-
for i in 0..<_SYMBOLIC_NAMES.count {
124+
let length = _SYMBOLIC_NAMES.count
125+
var tokenNames = [String?](count: length, repeatedValue: nil)
126+
for i in 0..<length {
126127
var name = VOCABULARY.getLiteralName(i)
127128
if name == nil {
128129
name = VOCABULARY.getSymbolicName(i)
129130
}
130-
131131
if name == nil {
132132
name = "<INVALID>"
133133
}
134-
tokenNames.append(name)
134+
tokenNames[i] = name
135135
}
136136
return tokenNames
137137
}()
@@ -141,15 +141,14 @@ public class SwiftLexer: Lexer {
141141
return tokenNames
142142
}
143143

144-
145144
public override func getVocabulary() -> Vocabulary {
146145
return SwiftLexer.VOCABULARY
147146
}
148147

149148
public override init(_ input: CharStream) {
150149
RuntimeMetaData.checkVersion("4.5.1", RuntimeMetaData.VERSION)
151150
super.init(input)
152-
_interp = LexerATNSimulator(self, SwiftLexer._ATN, SwiftLexer._decisionToDFA,_sharedContextCache)
151+
_interp = LexerATNSimulator(self, SwiftLexer._ATN, SwiftLexer._decisionToDFA, SwiftLexer._sharedContextCache)
153152
}
154153

155154
override
@@ -167,7 +166,7 @@ public class SwiftLexer: Lexer {
167166
override
168167
public func getATN() -> ATN { return SwiftLexer._ATN }
169168

170-
public static let _serializedATN: String = SwiftLexerATN().contents
169+
public static let _serializedATN: String = SwiftLexerATN().jsonString
171170
public static let _ATN: ATN = ATNDeserializer().deserializeFromJson(_serializedATN)
172171

173172
}

SwiftGrammar/Sources/Swift/AntlrGenerated/SwiftLexerATN.swift

+2-2
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)