-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodelicafmt.go
547 lines (474 loc) · 16.4 KB
/
modelicafmt.go
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright (c) 2020, Alliance for Sustainable Energy, LLC.
// All rights reserved.
package main
import (
"bufio"
"io"
"io/ioutil"
"strings"
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/urbanopt/modelica-fmt/thirdparty/parser"
)
type Config struct {
maxLineLength int
emptyLines bool
}
const (
// indent
spaceIndent = " "
)
// insertIndentBefore returns true if the rule should be on a new line and indented
func (l *modelicaListener) insertIndentBefore(rule antlr.ParserRuleContext) bool {
switch rule.(type) {
case
parser.IElementContext,
parser.IEquationsContext,
parser.IAlgorithm_statementsContext,
parser.IControl_structure_bodyContext,
parser.IAnnotationContext,
parser.IExpression_listContext,
parser.IConstraining_clauseContext,
parser.IIf_expressionContext,
parser.IIf_expression_bodyContext,
parser.IExternal_function_call_argumentContext:
return true
case parser.IString_commentContext:
return 0 == l.inAnnotation
case
parser.IArgumentContext,
parser.INamed_argumentContext:
return 0 == l.inAnnotation || 0 < l.inModelAnnotation
case parser.IExpressionContext:
if len(l.modelAnnotationVectorStack) == 0 {
return false
}
// handle expression which is an element of a vector (array_arguments) and within model annotation
arrayArgumentsNode, ok := rule.GetParent().(*parser.Array_argumentsContext)
if !ok {
return false
}
// check if the vector is the same as the one on top of our stack
thisVectorInterval := arrayArgumentsNode.GetParent().(*parser.VectorContext).GetSourceInterval()
stackVectorInterval := l.modelAnnotationVectorStack[len(l.modelAnnotationVectorStack)-1].GetSourceInterval()
if thisVectorInterval.Start == stackVectorInterval.Start && thisVectorInterval.Stop == stackVectorInterval.Stop {
return true
}
return false
case parser.IFunction_argumentContext:
return 0 == l.inNamedArgument && 0 == l.inVector && (0 == l.inAnnotation || 0 < l.inModelAnnotation)
default:
return false
}
}
// insertSpaceBeforeToken returns true if a space should be inserted before the current token
func insertSpaceBeforeToken(currentTokenText, previousTokenText string) bool {
switch currentTokenText {
case "(":
// add a space between 'annotation' and opening parens
if previousTokenText == "annotation" {
return true
}
fallthrough
default:
return !tokenInGroup(previousTokenText, noSpaceAfterTokens) &&
!tokenInGroup(currentTokenText, noSpaceBeforeTokens)
}
}
// insertNewlineBefore returns true if the rule should be on a new line
func insertNewlineBefore(rule antlr.ParserRuleContext) bool {
switch rule.(type) {
case
parser.ICompositionContext,
parser.IEquationsContext,
parser.IIf_expression_conditionContext,
parser.IElseif_expression_conditionContext,
parser.IElse_expression_conditionContext:
return true
default:
return false
}
}
var (
// tokens which should *generally* not have a space after them
// this can be overridden in the insertSpace function
noSpaceAfterTokens = []string{
"(",
"=",
".",
"[",
"{",
"-", "+", "^", "*", "/",
";",
",",
":", // array range constructor
}
// tokens which should *generally* not have a space before them
// this can be overridden in the insertSpace function
noSpaceBeforeTokens = []string{
"(", ")",
"[", "]",
"}",
";",
"=",
",",
".",
"-", "+", "^", "*", "/",
":", // array range constructor
}
allowBreakAfterTokens = []string{
"=",
",",
"(",
}
)
// tokenInGroup returns true if a token is in a given list
func tokenInGroup(token string, group []string) bool {
for _, other := range group {
if token == other {
return true
}
}
return false
}
type indent int
const (
renderIndent indent = iota
ignoreIndent
)
// modelicaListener is used to format the parse tree
type modelicaListener struct {
*parser.BaseModelicaListener // parser
writer *bufio.Writer // writing destination
indentationStack []indent // a stack used for tracking rendered and ignored indentations
onNewLine bool // true when write position succeeds a newline character
withinOnCurrentLine bool // true when `within` statement is found on the current line
insideBracket bool // true when inside brackets (i.e. `[]`)
lineIndentIncreased bool // true when the indentation level has already been increased for a line
previousTokenText string // text of previous token
previousTokenIdx int // index of previous token
commentTokens []antlr.Token // stores comments to insert while writing
maxLineLength int // configuration for num charaters per line
currentLineLength int // length of the line up to the writing position
// modelAnnotationVectorStack is a stack which stores `vector` contexts,
// which is used for conditionally indenting vector children
// Specifically, a vector inside of a model annotation will have indented elements
// if that vector has one or more elements which are function calls, class modifications or similar
// (ie not if all elements are numbers, more vectors, etc)
//
// The last element of the slice is the first `vector` context ancestor whose contents
// must be indented on new lines
// For example, we would like model annotations to look like this:
// annotation (
// Abc(
// paramA={
// SomeIdentifier(
// 1,
// 2),
// 123}))
//
// However, due to existing rules, we would end up with something like this
// annotation (
// Abc(
// paramA={SomeIdentifier(
// 1,
// 2), 123}))
//
// Thus by pushing/popping vectors we can check if an expression in a vector
// should be indented or not by checking if the top of the stack is its ancestor
modelAnnotationVectorStack []antlr.RuleContext
// NOTE: consider refactoring this simple approach for context awareness with
// a set.
// It should probably be map[string]int for rule name and current count (rules can be recursive, ie inside the same rule multiple times)
inAnnotation int // counts number of current or ancestor contexts that are annotation rule
inModelAnnotation int // counts number of current or ancestor contexts that are model annotation rule
inNamedArgument int // counts number of current or ancestor contexts that are named argument
inVector int // counts number of current or ancestor contexts that are vector
inLastSemicolon bool // true if the listener is handling the last_semicolon rule
// Other config
config Config
}
func newListener(out io.Writer, commentTokens []antlr.Token, config Config) *modelicaListener {
return &modelicaListener{
BaseModelicaListener: &parser.BaseModelicaListener{},
writer: bufio.NewWriter(out),
onNewLine: true,
withinOnCurrentLine: false,
insideBracket: false,
lineIndentIncreased: false,
inLastSemicolon: false,
inAnnotation: 0,
inModelAnnotation: 0,
inVector: 0,
inNamedArgument: 0,
previousTokenText: "",
previousTokenIdx: -1,
commentTokens: commentTokens,
currentLineLength: 0,
config: config,
}
}
func (l *modelicaListener) close() {
err := l.writer.Flush()
if err != nil {
panic(err)
}
}
// indentation returns the writer's current number of *rendered* indentations
func (l *modelicaListener) indentation() int {
nRenderIndents := 0
for _, indentType := range l.indentationStack {
if indentType == renderIndent {
nRenderIndents++
}
}
return nRenderIndents
}
// maybeIndent should be called when the writer's indentation is to be increased
func (l *modelicaListener) maybeIndent() {
// Only increase indentation if it hasn't been changed already, otherwise ignore it
// NOTE: This means that there can be at most 1 increase in indentation per line
// This is a bit of a hack to avoid having an overindented line, occurring when
// multiple rules want to be indented and we want it to be indented only once
if !l.lineIndentIncreased {
l.indentationStack = append(l.indentationStack, renderIndent)
// WARNING: this is coupled with writeNewline, which should reset
// lineIndentIncreased to false
l.lineIndentIncreased = true
} else {
l.indentationStack = append(l.indentationStack, ignoreIndent)
}
}
// maybeDedent should be called when the writer's indentation is to be decreased
func (l *modelicaListener) maybeDedent() {
l.indentationStack = l.indentationStack[:len(l.indentationStack)-1]
}
// writeString writes a string to the listener's output
// It should serve as the main entrypoint to writing to the output
func (l *modelicaListener) writeString(str string) {
originalSpacePrefix := l.getSpaceBefore(str, true)
charsOnFirstLine := len(originalSpacePrefix)
firstNewlineIndex := strings.Index(str, "\n")
if firstNewlineIndex < 0 {
charsOnFirstLine += len(str)
} else {
charsOnFirstLine += firstNewlineIndex
}
// break the line if writing this string would make it too long and the previous token is breakable
var actualSpacePrefix string
if l.config.maxLineLength > 0 &&
l.currentLineLength+charsOnFirstLine > l.config.maxLineLength &&
tokenInGroup(l.previousTokenText, allowBreakAfterTokens) {
l.writeNewline()
l.maybeIndent()
actualSpacePrefix = l.getSpaceBefore(str, false)
l.writer.WriteString(actualSpacePrefix + str)
l.maybeDedent()
} else {
actualSpacePrefix = l.getSpaceBefore(str, false)
l.writer.WriteString(actualSpacePrefix + str)
}
lastNewlineIndex := strings.LastIndex(str, "\n")
var charsOnLastLine int
if lastNewlineIndex < 0 {
charsOnLastLine = len(actualSpacePrefix) + len(str)
} else {
// since there was a newline, no need to add the space prefix to the count
charsOnLastLine = len(str) - (lastNewlineIndex + 1)
}
l.currentLineLength += charsOnLastLine
}
func (l *modelicaListener) writeNewline() {
// explicitly not using l.writeString here b/c it's not necessary and I think we could end up in infinite recursion (though really unlikely)
l.writer.WriteString("\n")
l.onNewLine = true
l.currentLineLength = 0
// WARNING: this is coupled with maybeIndent, which uses this state
l.lineIndentIncreased = false
}
func (l *modelicaListener) writeComment(comment antlr.Token) {
l.writeString(comment.GetText())
if comment.GetTokenType() == parser.ModelicaLexerLINE_COMMENT {
l.writeNewline()
}
}
// getSpaceBefore returns whitespace that should prefix the string. Note that this can modify the listener state
// If dryRun is true, the function will NOT modify the listener state (useful for predicting what the space will be)
func (l *modelicaListener) getSpaceBefore(str string, dryRun bool) string {
if l.onNewLine {
if !dryRun {
l.onNewLine = false
}
// insert indentation
if l.indentation() > 0 {
indentation := l.indentation()
return strings.Repeat(spaceIndent, indentation)
}
} else if insertSpaceBeforeToken(str, l.previousTokenText) {
// insert a space
return " "
}
return ""
}
// insertBlankLine returns true if an empty line should be inserted
// Used when visiting a terminal semicolon (ie ';')
func (l *modelicaListener) insertBlankLine() bool {
if !l.config.emptyLines {
return false
}
// if at the end of the file (ie the last semicolon) only insert an extra
// line if there are comments remaining which will be appended at the end of
// the file
if l.inLastSemicolon {
return len(l.commentTokens) > 0
}
// only insert a blank line if there's no `within` on current line,
// and we're outside of brackets
return !l.withinOnCurrentLine && !l.insideBracket
}
func (l *modelicaListener) VisitTerminal(node antlr.TerminalNode) {
// if there's a comment that should go before this node, insert it first
tokenIdx := node.GetSymbol().GetTokenIndex()
for len(l.commentTokens) > 0 && tokenIdx > l.commentTokens[0].GetTokenIndex() && l.commentTokens[0].GetTokenIndex() > l.previousTokenIdx {
commentToken := l.commentTokens[0]
l.commentTokens = l.commentTokens[1:]
l.writeComment(commentToken)
}
l.writeString(node.GetText())
if l.previousTokenText == "within" {
l.withinOnCurrentLine = true
}
if l.previousTokenText == "[" {
l.insideBracket = true
} else if l.previousTokenText == "]" {
l.insideBracket = false
}
if node.GetText() == ";" {
l.writeNewline()
if l.insertBlankLine() {
l.writeNewline()
} else {
l.withinOnCurrentLine = false
}
}
l.previousTokenText = node.GetText()
l.previousTokenIdx = node.GetSymbol().GetTokenIndex()
}
func (l *modelicaListener) EnterEveryRule(node antlr.ParserRuleContext) {
if insertNewlineBefore(node) && !l.onNewLine {
l.writeNewline()
}
if l.insertIndentBefore(node) {
if !l.onNewLine {
l.writeNewline()
}
l.maybeIndent()
}
}
func (l *modelicaListener) ExitEveryRule(node antlr.ParserRuleContext) {
if l.insertIndentBefore(node) {
l.maybeDedent()
}
}
func (l *modelicaListener) EnterAnnotation(node *parser.AnnotationContext) {
l.inAnnotation++
}
func (l *modelicaListener) ExitAnnotation(node *parser.AnnotationContext) {
l.inAnnotation--
}
func (l *modelicaListener) EnterModel_annotation(node *parser.Model_annotationContext) {
l.inModelAnnotation++
}
func (l *modelicaListener) ExitModel_annotation(node *parser.Model_annotationContext) {
l.inModelAnnotation--
}
func (l *modelicaListener) EnterVector(node *parser.VectorContext) {
l.inVector++
if l.inModelAnnotation > 0 {
// if this array uses an iterator for construction it gets no special treatment
if _, ok := node.GetChild(0).(parser.Array_iterator_constructorContext); ok {
return
}
// check if there is an element of this vector which would require indentation
for _, child := range node.Array_arguments().GetChildren() {
expressionNode, ok := child.(*parser.ExpressionContext)
if !ok {
continue
}
startToken := expressionNode.GetStart()
if startToken.GetTokenType() == parser.ModelicaLexerIDENT {
l.modelAnnotationVectorStack = append(l.modelAnnotationVectorStack, node)
break
}
}
}
}
func (l *modelicaListener) ExitVector(node *parser.VectorContext) {
l.inVector--
if len(l.modelAnnotationVectorStack) > 0 {
annotationVectorInterval := l.modelAnnotationVectorStack[len(l.modelAnnotationVectorStack)-1].GetSourceInterval()
thisVectorInterval := node.GetSourceInterval()
if annotationVectorInterval.Start == thisVectorInterval.Start && annotationVectorInterval.Stop == thisVectorInterval.Stop {
l.modelAnnotationVectorStack = l.modelAnnotationVectorStack[:len(l.modelAnnotationVectorStack)-1]
}
}
}
func (l *modelicaListener) EnterNamed_argument(node *parser.Named_argumentContext) {
l.inNamedArgument++
}
func (l *modelicaListener) ExitNamed_argument(node *parser.Named_argumentContext) {
l.inNamedArgument--
}
func (l *modelicaListener) EnterLast_semicolon(node *parser.Last_semicolonContext) {
l.inLastSemicolon = true
}
func (l *modelicaListener) ExitLast_semicolon(node *parser.Last_semicolonContext) {
l.inLastSemicolon = false
}
// commentCollector is a wrapper around the default lexer which collects comment
// tokens for later use
type commentCollector struct {
antlr.TokenSource
commentTokens []antlr.Token
}
func newCommentCollector(source antlr.TokenSource) commentCollector {
return commentCollector{
source,
[]antlr.Token{},
}
}
// NextToken returns the next token from the source
func (c *commentCollector) NextToken() antlr.Token {
token := c.TokenSource.NextToken()
tokenType := token.GetTokenType()
if tokenType == parser.ModelicaLexerCOMMENT || tokenType == parser.ModelicaLexerLINE_COMMENT {
c.commentTokens = append(c.commentTokens, token)
}
return token
}
// processFile formats a file
func processFile(filename string, out io.Writer, config Config) error {
content, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
text := string(content)
inputStream := antlr.NewInputStream(text)
lexer := parser.NewModelicaLexer(inputStream)
// wrap the default lexer to collect comments and set it as the stream's source
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
tokenSource := newCommentCollector(lexer)
stream.SetTokenSource(&tokenSource)
p := parser.NewModelicaParser(stream)
sd := p.Stored_definition()
listener := newListener(out, tokenSource.commentTokens, config)
defer listener.close()
antlr.ParseTreeWalkerDefault.Walk(listener, sd)
// add any remaining comments and handle newline at end of file
for _, comment := range listener.commentTokens {
listener.writeComment(comment)
}
if !listener.onNewLine {
listener.writeNewline()
}
return nil
}