Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4eaa0471046cb9c9c476f4565b78551d3c36b4e3 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*******************************************************************************
 * Copyright (c) 2018 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.dctools.fsm.ast

import java.util.ArrayList
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstArrayAccessNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstBracketNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstBracketNode.BracketType
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstFeatureCallNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstIdentifierBracketNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstIdentifierNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstMatchNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstOperationCallNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstOtherNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstPeriodNode
import org.eclipse.etrice.dctools.fsm.ast.nodes.DCAstWhitespaceNode
import org.eclipse.etrice.dctools.fsm.ast.tokens.DCBracketToken
import org.eclipse.etrice.dctools.fsm.ast.tokens.DCBracketToken.BracketKind
import org.eclipse.etrice.dctools.fsm.ast.tokens.DCTextToken
import org.eclipse.etrice.dctools.fsm.ast.tokens.DCToken.Kind

/**
 * 
 * Grammar:
 * 
 * PartialMatch: BracketExpression | FeatureCall
 * 
 * BracketExpression: RoundBracketExpression | CurlyBracketExpression | SquareBracketExpression
 * RoundBracketExpression: LEFT_ROUNDBRACKET Match RIGHT_ROUNDBRACKET
 * CurlyBracketExpression: LEFT_CURLYBRACKET Match RIGHT_CORLYBRACKET
 * SquareBracketExpression: LEFT_SQUAREBRACKET Match RIGHT_SQUAREBRACKET
 * 
 * FeatureCall: Call Whitespace (PERIOD Whitespace Call Whitespace)*
 * Call: ArrayAccess | OperationCall | IDENTIFIER
 * ArrayAccess: IDENTIFIER Whitespace SquareBracketExpression
 * OperationCall: IDENTIFIER Whitespace RoundBracketExpression
 * 
 * Whitespace: WHITESPACE*
 * 
 * Match: (PartialMatch* Other)*
 * 
 * Other: *
 */
class DCParser {
	
	val DCScanner scanner
	String text
	ArrayList<DCTextToken> tokens
	int currentPosition = 0
	
	new(DCLanguage language) {
		scanner = new DCScanner(language)
	}
	
	def parse(String text) {
		this.text = text
		this.tokens = scanner.scan(text)
		
		Match(null)
	}
	
	/*
	 * rules
	 */
	private def DCAstMatchNode Match(DCAstNode parent) {
		val mNode = new DCAstMatchNode(parent)
		
		var continue = true
		while (continue) {
			while (PartialMatch(mNode)!==null) {}
			if (Other(mNode)===null) {
				continue = false
			}
		}
		
		return mNode
	}
	
	private def DCAstOtherNode Other(DCAstNode parent) {
		var DCAstOtherNode lastNode = null
		var token = read
		
		if (token!==null) {
			while (token!==null) {
				if (stopOther(token)) {
					unread
					token = null
				}
				else {
					// we consumed a token
					lastNode = new DCAstOtherNode(parent, text.substring(token.begin, token.begin + token.length), token)
					
					// do we have a partial match after that token?
					val pNode = PartialMatch(null)
					if (pNode!==null) {
						unread(pNode)
						token = null
					}
					else {
						token = read
					}
				}
			}
		}
		
		return lastNode
	}
	
	private def boolean stopOther(DCTextToken token) {
		if (token.token instanceof DCBracketToken) {
			return true
		}
		if (token.token.kind==Kind.IDENTIFIER) {
			return true
		}
		return false
	}
	
	private def DCAstNode PartialMatch(DCAstNode parent) {
		var node = ConcreteBracketExpression(parent) as DCAstNode
		if (node===null) {
			node = FeatureCall(parent)
		}
		return node
	}
	
	private def DCAstBracketNode ConcreteBracketExpression(DCAstNode parent) {
		var node = RoundBracketExpression(parent)
		if (node===null) {
			node = CurlyBracketExpression(parent)
		}
		if (node===null) {
			node = SquareBracketExpression(parent)
		}
		return node
	}
	
	private def DCAstBracketNode RoundBracketExpression(DCAstNode parent) {
		return ConcreteBracketExpression(parent, BracketKind.LEFT_ROUND, BracketKind.RIGHT_ROUND, BracketType.ROUND)
	}
	
	private def DCAstBracketNode CurlyBracketExpression(DCAstNode parent) {
		return ConcreteBracketExpression(parent, BracketKind.LEFT_CURLY, BracketKind.RIGHT_CURLY, BracketType.CURLY)
	}
	
	private def DCAstBracketNode SquareBracketExpression(DCAstNode parent) {
		return ConcreteBracketExpression(parent, BracketKind.LEFT_SQUARE, BracketKind.RIGHT_SQUARE, BracketType.SQUARE)
	}
	
	private def DCAstBracketNode ConcreteBracketExpression(DCAstNode parent, BracketKind left, BracketKind right, BracketType type) {
		var token = read
		
		if (token!==null) {
			if (token.token instanceof DCBracketToken) {
				var b = token.token as DCBracketToken
				if (b.bracketKind==left) {
					val contents = Match(parent)
					token = read
					if (token!==null) {
						if (token.token instanceof DCBracketToken) {
							b = token.token as DCBracketToken
							if (b.bracketKind==right) {
								return new DCAstBracketNode(parent, type, contents)
							}
						}
						unread // second token
					}
					else {
						// we reached the end without a matching right bracket
						return new DCAstBracketNode(parent, type, contents, false)
					}
					unread(contents)
				}
			}
			unread // first token
		}
		
		return null
	}
	
	private def DCAstFeatureCallNode FeatureCall(DCAstNode parent) {
		val callNode = Call(parent)
		
		if (callNode!==null) {
			val fcNode = new DCAstFeatureCallNode(parent)
			callNode.parent = fcNode
			
			// optional whitespace
			Whitespace(fcNode)
			
			// (PERIOD Whitespace Call)*
			var token = read
			while (token!==null) {
				if (token.token.kind==Kind.PERIOD) {
					val pNode = new DCAstPeriodNode(null)
					val wsNode = Whitespace(null)
					val cNode = Call(null)
					val wsNode2 = Whitespace(null)
					if (pNode!==null) {
						pNode.parent = fcNode
						if (wsNode!==null) {
							wsNode.parent = fcNode
						}
						if (cNode!==null) {
							cNode.parent = fcNode
						} 
						if (wsNode2!==null) {
							wsNode2.parent = fcNode
						}
						
						if (cNode!==null) {
							// next token
							token = read
						}
						else {
							token = null
						}
					}
					else {
						unread(pNode)
						unread(wsNode)
						unread(pNode)
						unread(wsNode2)
						unread // the token itself
						token = null
					}
				}
				else {
					unread // the token itself
					token = null
				}
			}
			
			return fcNode
		}
		
		unread(callNode)
		
		return null
	}
	
	private def DCAstNode Call(DCAstNode parent) {
		// try ArrayAccess
		var node = BracketExpressionCall(parent, [SquareBracketExpression], [p, i, w, b| new DCAstArrayAccessNode(p, i, w, b)])
		if (node===null) {
			// try OperationCall
			node = BracketExpressionCall(parent, [RoundBracketExpression], [p, i, w, b| new DCAstOperationCallNode(p, i, w, b)])
		}
		if (node!==null) {
			return node
		}
		else {
			// try IDENTIFIER
			val token = read
			if (token!==null) {
				if (token.token.kind==Kind.IDENTIFIER) {
					val id = text.substring(token.begin, token.begin + token.length)
					return new DCAstIdentifierNode(parent, id, token)
				}
				unread
			}
		}
		return null
	}
	
	private def DCAstIdentifierBracketNode BracketExpressionCall(
		DCAstNode parent,
		(DCAstNode)=>DCAstBracketNode bracketExpression,
		(DCAstNode, DCAstIdentifierNode, DCAstWhitespaceNode, DCAstBracketNode)=>DCAstIdentifierBracketNode factory
	) {
		val token = read
		if (token!==null) {
			if (token.token.kind==Kind.IDENTIFIER) {
				// first child is ID
				val id = text.substring(token.begin, token.begin + token.length)
				val idNode = new DCAstIdentifierNode(parent, id, token)
				
				// optional whitespace
				val wsNode = Whitespace(parent)
				
				// second child is BracketExpression
				val node = bracketExpression.apply(parent)
				if (node!==null) {
					// the node of the bracket expression call
					val callNode = factory.apply(parent, idNode, wsNode, node)
					return callNode
				}
				
				unread(idNode)
				unread(wsNode)
			}
			else {
				unread
			}
		}
		
		return null
	}
	
	private def DCAstWhitespaceNode Whitespace(DCAstNode parent) {
		var token = read
		val begin = if (token!==null) token.begin else 0
		var int end = 0
		var count = 0
		while (token!==null) {
			if (token.token.kind==Kind.WHITESPACE) {
				end = token.begin + token.length
				count++
				token = read
			}
			else {
				token = null
				unread
			}
		}
		
		if (count>0) {
			val ws = text.substring(begin, end)
			return new DCAstWhitespaceNode(parent, count, ws)
		}
		
		return null
	}
	
	/*
	 * helpers
	 */
	private def DCTextToken read() {
		if (currentPosition<tokens.size) {
			tokens.get(currentPosition++)
		}
		else {
			null
		}
	}
	
	private def void unread() {
		unread(1)
	}
	
	private def void unread(int i) {
		currentPosition = currentPosition - i
		if (currentPosition<0) {
			println("oops")
		}
	}
	
	private def void unread(DCAstNode node) {
		if (node!==null) {
			node.parent = null
			unread(node.totallyReadTokens)
		}
	}
}

Back to the top