-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoParser.java
2456 lines (2250 loc) · 77.8 KB
/
AcceleoParser.java
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2016, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.parser;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.eclipse.acceleo.AcceleoASTNode;
import org.eclipse.acceleo.AcceleoPackage;
import org.eclipse.acceleo.Binding;
import org.eclipse.acceleo.Block;
import org.eclipse.acceleo.BlockComment;
import org.eclipse.acceleo.Comment;
import org.eclipse.acceleo.CommentBody;
import org.eclipse.acceleo.Documentation;
import org.eclipse.acceleo.Error;
import org.eclipse.acceleo.ErrorBinding;
import org.eclipse.acceleo.ErrorComment;
import org.eclipse.acceleo.ErrorExpressionStatement;
import org.eclipse.acceleo.ErrorFileStatement;
import org.eclipse.acceleo.ErrorForStatement;
import org.eclipse.acceleo.ErrorIfStatement;
import org.eclipse.acceleo.ErrorImport;
import org.eclipse.acceleo.ErrorLetStatement;
import org.eclipse.acceleo.ErrorMetamodel;
import org.eclipse.acceleo.ErrorModule;
import org.eclipse.acceleo.ErrorModuleDocumentation;
import org.eclipse.acceleo.ErrorModuleElementDocumentation;
import org.eclipse.acceleo.ErrorModuleReference;
import org.eclipse.acceleo.ErrorProtectedArea;
import org.eclipse.acceleo.ErrorQuery;
import org.eclipse.acceleo.ErrorTemplate;
import org.eclipse.acceleo.ErrorVariable;
import org.eclipse.acceleo.Expression;
import org.eclipse.acceleo.ExpressionStatement;
import org.eclipse.acceleo.FileStatement;
import org.eclipse.acceleo.ForStatement;
import org.eclipse.acceleo.IfStatement;
import org.eclipse.acceleo.Import;
import org.eclipse.acceleo.LeafStatement;
import org.eclipse.acceleo.LetStatement;
import org.eclipse.acceleo.Metamodel;
import org.eclipse.acceleo.Module;
import org.eclipse.acceleo.ModuleDocumentation;
import org.eclipse.acceleo.ModuleElement;
import org.eclipse.acceleo.ModuleElementDocumentation;
import org.eclipse.acceleo.ModuleReference;
import org.eclipse.acceleo.NewLineStatement;
import org.eclipse.acceleo.OpenModeKind;
import org.eclipse.acceleo.ParameterDocumentation;
import org.eclipse.acceleo.ProtectedArea;
import org.eclipse.acceleo.Query;
import org.eclipse.acceleo.Statement;
import org.eclipse.acceleo.Template;
import org.eclipse.acceleo.TextStatement;
import org.eclipse.acceleo.Variable;
import org.eclipse.acceleo.VisibilityKind;
import org.eclipse.acceleo.aql.AcceleoUtil;
import org.eclipse.acceleo.query.AQLUtils;
import org.eclipse.acceleo.query.AQLUtils.AcceleoAQLResult;
import org.eclipse.acceleo.query.ast.ASTNode;
import org.eclipse.acceleo.query.parser.AstResult;
import org.eclipse.acceleo.query.parser.Positions;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
/**
* Acceleo parser.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class AcceleoParser {
/**
* The acceleo URI protocol.
*/
public static final String ACCELEOENV_URI_PROTOCOL = "acceleoenv::";
/**
* The module file extension.
*/
public static final String MODULE_FILE_EXTENSION = "mtl";
/**
* New line.
*/
public static final String NEW_LINE = "\n";
/**
* In line end delimiter.
*/
public static final String NO_SLASH_END = "]";
/**
* In line end delimiter.
*/
public static final String SLASH_END = "/]";
/**
* End of {@link TextStatement}.
*/
public static final String TEXT_END = "[";
/**
* Empty {@link ExpressionStatement}.
*/
public static final String EMPTY_EXPRESSION_STATEMENT = TEXT_END + SLASH_END;
/**
* Start of {@link BlockComment}.
*/
public static final String BLOCK_COMMENT_START = "[comment]";
/**
* End of {@link BlockComment}.
*/
public static final String BLOCK_COMMENT_END = "[/comment]";
/**
* Start of {@link Comment}.
*/
public static final String COMMENT_START = "[comment ";
/**
* End of {@link Comment}.
*/
public static final String COMMENT_END = SLASH_END;
/**
* Start of {@link Module} header.
*/
public static final String MODULE_HEADER_START = "[module ";
/**
* End of {@link Module} header.
*/
public static final String MODULE_HEADER_END = SLASH_END;
/**
* Open parenthesis.
*/
public static final String OPEN_PARENTHESIS = "(";
/**
* Close parenthesis.
*/
public static final String CLOSE_PARENTHESIS = ")";
/**
* A comma.
*/
public static final String COMMA = ",";
/**
* A colon.
*/
public static final String COLON = ":";
/**
* A qualifier separator.
*/
public static final String QUALIFIER_SEPARATOR = "::";
/**
* An equal.
*/
public static final String EQUAL = "=";
/**
* A pipe.
*/
public static final String PIPE = "|";
/**
* A quote.
*/
public static final String QUOTE = "'";
/**
* Start of {@link Template} header.
*/
public static final String TEMPLATE_HEADER_START = "[template ";
/**
* Start of {@link Template} header.
*/
public static final String TEMPLATE_HEADER_END = NO_SLASH_END;
/**
* Guard of {@link Template}.
*/
public static final String TEMPLATE_GUARD = "?";
/**
* Post of {@link Template}.
*/
public static final String TEMPLATE_POST = "post(";
/**
* End block prefix.
*/
public static final String END_BLOCK_PREFIX = "[/";
/**
* End of {@link Template}.
*/
public static final String TEMPLATE_END = END_BLOCK_PREFIX + "template]";
/**
* Start of {@link ExpressionStatement}.
*/
public static final String EXPRESSION_STATEMENT_START = "[";
/**
* End of {@link ExpressionStatement}.
*/
public static final String EXPRESSION_STATEMENT_END = SLASH_END;
/**
* Start of {@link ProtectedArea} header.
*/
public static final String PROTECTED_AREA_HEADER_START = "[protected ";
/**
* End of {@link ProtectedArea} header.
*/
public static final String PROTECTED_AREA_HEADER_END = NO_SLASH_END;
/**
* {@link ProtectedArea} start tag prefix.
*/
public static final String PROTECTED_AREA_START_TAG_PREFIX = "startTagPrefix(";
/**
* {@link ProtectedArea} end tag prefix.
*/
public static final String PROTECTED_AREA_END_TAG_PREFIX = "endTagPrefix(";
/**
* End of {@link ProtectedArea}.
*/
public static final String PROTECTED_AREA_END = END_BLOCK_PREFIX + "protected]";
/**
* Start of {@link FileStatement} header.
*/
public static final String FILE_HEADER_START = "[file ";
/**
* End of {@link FileStatement} header.
*/
public static final String FILE_HEADER_END = NO_SLASH_END;
/**
* End of {@link FileStatement}.
*/
public static final String FILE_END = END_BLOCK_PREFIX + "file]";
/**
* Start of {@link LetStatement} header.
*/
public static final String LET_HEADER_START = "[let ";
/**
* End of {@link LetStatement} header.
*/
public static final String LET_HEADER_END = NO_SLASH_END;
/**
* End of {@link LetStatement}.
*/
public static final String LET_END = END_BLOCK_PREFIX + "let]";
/**
* Start of {@link ForStatement} header.
*/
public static final String FOR_HEADER_START = "[for ";
/**
* Start of {@link ForStatement} header.
*/
public static final String FOR_SEPARATOR = "separator(";
/**
* End of {@link ForStatement} header.
*/
public static final String FOR_HEADER_END = NO_SLASH_END;
/**
* End of {@link ForStatement}.
*/
public static final String FOR_END = END_BLOCK_PREFIX + "for]";
/**
* Start of {@link IfStatement} header.
*/
public static final String IF_HEADER_START = "[if ";
/**
* End of {@link IfStatement} header.
*/
public static final String IF_HEADER_END = NO_SLASH_END;
/**
* End of {@link IfStatement}.
*/
public static final String IF_ELSEIF = "[elseif ";
/**
* End of {@link IfStatement}.
*/
public static final String IF_ELSE = "[else]";
/**
* End of {@link IfStatement}.
*/
public static final String IF_END = END_BLOCK_PREFIX + "if]";
/**
* Start of {@link Query}.
*/
public static final String QUERY_START = "[query ";
/**
* End of {@link Query}.
*/
public static final String QUERY_END = SLASH_END;
/**
* Start of {@link Import}.
*/
public static final String IMPORT_START = "[import ";
/**
* End of {@link Import}.
*/
public static final String IMPORT_END = SLASH_END;
/**
* Start of {@link Documentation}.
*/
public static final String DOCUMENTATION_START = "[**";
/**
* End of {@link Documentation}.
*/
public static final String DOCUMENTATION_END = SLASH_END;
/**
* Main tag.
*/
public static final String MAIN_TAG = "@main";
/**
* Author tag.
*/
public static final String AUTHOR_TAG = "@author ";
/**
* Version tag.
*/
public static final String VERSION_TAG = "@version ";
/**
* Since tag.
*/
public static final String SINCE_TAG = "@since ";
/**
* Param tag.
*/
public static final String PARAM_TAG = "@param ";
/**
* Extends key work.
*/
public static final String EXTENDS = "extends ";
/**
* The mandatory indentation for not inlined {@link Block}.
*/
public static final int INDENTATION = 2;
/**
* The {@link Positions}.
*/
private Positions<ASTNode> positions;
/**
* The parser currentPosition.
*/
private int currentPosition;
/**
* The line number at a given position.
*/
private int[] lines;
/**
* The column number at a given position.
*/
private int[] columns;
/**
* The source text.
*/
private String text;
/**
* The {@link List} of {@link Error}.
*/
private List<Error> errors;
/**
* Parses the given {@link InputStream}.
*
* @param source
* the {@link InputStream}
* @param charset
* the {@link Charset}
* @param moduleQualifiedName
* the qualified name of the {@link Module} (e.g. "path::to::module").
* @return the parsed {@link AstResult}
* @throws IOException
* if the {@link InputStream} can't be read
*/
public AcceleoAstResult parse(InputStream source, Charset charset, String moduleQualifiedName)
throws IOException {
return parse(AcceleoUtil.getContent(source, charset.name()), moduleQualifiedName);
}
/**
* Parses the given source text.
*
* @param source
* the source text
* @param qualifiedName
* the qualified name of the {@link Module} (e.g. "path::to::module").
* @return the parsed {@link AstResult}
*/
public AcceleoAstResult parse(String source, String qualifiedName) {
this.currentPosition = 0;
this.lines = new int[source.length() + 1];
this.columns = new int[source.length() + 1];
this.positions = new Positions<>();
this.text = source;
computeLinesAndColumns(text);
this.errors = new ArrayList<Error>();
final List<Comment> comments = parseCommentsOrModuleDocumentations();
final Module module = parseModule(comments);
final Resource containerEmfResource = new XMIResourceImpl(URI.createURI(ACCELEOENV_URI_PROTOCOL
+ qualifiedName));
containerEmfResource.getContents().add(module);
return new AcceleoAstResult(module, positions, errors);
}
/**
* Computes the lines and columns at given position.
*
* @param str
* the source
*/
// TODO do it on the fly to prevent reading the input twice
private void computeLinesAndColumns(String str) {
int currentLine = 0;
int currentColumn = 0;
for (int i = 0; i < str.length(); i++) {
lines[i] = currentLine;
columns[i] = currentColumn;
char currentCharacter = str.charAt(i);
if (currentCharacter == '\n') {
currentLine++;
currentColumn = 0;
} else {
currentColumn++;
}
}
// User cursor may be at the position right after the last character of the String.
lines[str.length()] = currentLine;
columns[str.length()] = currentColumn;
}
/**
* Sets the identifier start and identifer end positions for the given {@link AcceleoASTNode}.
*
* @param node
* the {@link AcceleoASTNode}
* @param start
* the start position
* @param end
* the end position
*/
private void setIdentifierPositions(AcceleoASTNode node, int start, int end) {
positions.setIdentifierStartPositions(node, start);
positions.setIdentifierStartLines(node, lines[start]);
positions.setIdentifierStartColumns(node, columns[start]);
positions.setIdentifierEndPositions(node, end);
positions.setIdentifierEndLines(node, lines[end]);
positions.setIdentifierEndColumns(node, columns[end]);
}
/**
* Sets the start and end positions for the given {@link AcceleoASTNode}.
*
* @param node
* the {@link AcceleoASTNode}
* @param start
* the start position
* @param end
* the end position
*/
private void setPositions(AcceleoASTNode node, int start, int end) {
positions.setStartPositions(node, start);
positions.setStartLines(node, lines[start]);
positions.setStartColumns(node, columns[start]);
positions.setEndPositions(node, end);
positions.setEndLines(node, lines[end]);
positions.setEndColumns(node, columns[end]);
}
/**
* Parses a {@link List} of {@link Comment} and {@link ModuleDocumentation}.
*
* @return the created {@link List} of {@link Comment} and {@link ModuleDocumentation}
*/
protected List<Comment> parseCommentsOrModuleDocumentations() {
final List<Comment> comments = new ArrayList<Comment>();
Comment comment = parseComment();
ModuleDocumentation documentation = parseModuleDocumentation();
while (comment != null || documentation != null) {
if (comment != null) {
comments.add(comment);
}
if (documentation != null) {
comments.add(documentation);
}
skipSpaces();
comment = parseComment();
documentation = parseModuleDocumentation();
}
return comments;
}
/**
* Skips {@link Character#isWhitespace(char) white spaces}.
*/
protected void skipSpaces() {
while (currentPosition < text.length() && Character.isWhitespace(text.charAt(currentPosition))) {
currentPosition++;
}
}
/**
* Reads the given {@link String}.
*
* @param str
* the {@link String} to read
* @return <code>true</code> if the given {@link String} is at the current position, <code>false</code>
* otherwise
*/
protected boolean readString(String str) {
final boolean res = text.startsWith(str, currentPosition);
if (res) {
currentPosition += str.length();
}
return res;
}
/**
* {@link AcceleoParser#readString(String) Reads} the given {@link String}.
*
* @param str
* the {@link String} to read
* @return <code>-1</code> if {@link AcceleoParser#readString(String) read} the given {@link String}
* succeed, the current position otherwise
*/
protected int readMissingString(String str) {
final int res;
if (readString(str)) {
res = -1;
} else {
res = currentPosition;
}
return res;
}
/**
* Parses and identifier.
*
* @return the identifier if any recognized, <code>null</code> otherwise
*/
protected String parseIdentifier() {
final String res;
if (currentPosition < text.length() && Character.isJavaIdentifierStart(text.charAt(
currentPosition))) {
final int identifierStart = currentPosition;
currentPosition++;
while (currentPosition < text.length() && Character.isJavaIdentifierPart(text.charAt(
currentPosition))) {
currentPosition++;
}
res = text.substring(identifierStart, currentPosition);
} else {
res = null;
}
return res;
}
/**
* Parses a {@link Comment} or a {@link BlockComment}.
*
* @return the created {@link Comment} or {@link BlockComment} if any recognized, <code>null</code>
* otherwise
*/
protected Comment parseComment() {
final Comment res;
if (text.startsWith(BLOCK_COMMENT_START, currentPosition)) {
res = createComment(BLOCK_COMMENT_START, BLOCK_COMMENT_END, AcceleoPackage.eINSTANCE
.getBlockComment(), AcceleoPackage.eINSTANCE.getErrorBlockComment());
} else if (text.startsWith(COMMENT_START, currentPosition)) {
res = createComment(COMMENT_START, COMMENT_END, AcceleoPackage.eINSTANCE.getComment(),
AcceleoPackage.eINSTANCE.getErrorComment());
} else {
res = null;
}
return res;
}
/**
* Creates the {@link Comment} or {@link ErrorComment}.
*
* @param startTag
* the start tag
* @param endTag
* the end tag
* @param commentClass
* the {@link Comment} {@link EClass}
* @param errorCommentClass
* the {@link ErrorComment} {@link EClass}
* @return the {@link Comment} or {@link ErrorComment}
*/
private Comment createComment(String startTag, String endTag, EClass commentClass,
EClass errorCommentClass) {
final Comment res;
final int startPosition = currentPosition;
final int startOfCommentBody = currentPosition + startTag.length();
int endOfCommentBody = getNext(endTag);
if (endOfCommentBody < 0) {
endOfCommentBody = text.length();
res = (Comment)EcoreUtil.create(errorCommentClass);
setPositions(res, startPosition, endOfCommentBody);
((ErrorComment)res).setMissingEndHeader(endOfCommentBody);
errors.add((Error)res);
currentPosition = endOfCommentBody;
} else {
res = (Comment)EcoreUtil.create(commentClass);
setPositions(res, startPosition, endOfCommentBody + endTag.length());
currentPosition = endOfCommentBody + endTag.length();
}
final CommentBody commentBody = AcceleoPackage.eINSTANCE.getAcceleoFactory().createCommentBody();
commentBody.setValue(text.substring(startOfCommentBody, endOfCommentBody));
setPositions(commentBody, startOfCommentBody, endOfCommentBody);
res.setBody(commentBody);
return res;
}
/**
* Parses a {@link ModuleDocumentation}.
*
* @return the created {@link ModuleDocumentation} if any recognized, <code>null</code> otherwise
*/
protected ModuleDocumentation parseModuleDocumentation() {
final ModuleDocumentation res;
if (text.startsWith(DOCUMENTATION_START, currentPosition)) {
final int commentStartPositon = currentPosition;
currentPosition += DOCUMENTATION_START.length();
final int startPosition = currentPosition;
int endPosition = getNext(DOCUMENTATION_END);
if (endPosition < 0) {
endPosition = text.length();
currentPosition = endPosition;
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createErrorModuleDocumentation();
((ErrorModuleDocumentation)res).setMissingEndHeader(endPosition);
errors.add((Error)res);
} else {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createModuleDocumentation();
currentPosition = endPosition + DOCUMENTATION_END.length();
}
final String docString = text.substring(startPosition, endPosition);
final CommentBody commentBody = AcceleoPackage.eINSTANCE.getAcceleoFactory().createCommentBody();
setPositions(commentBody, startPosition, endPosition);
commentBody.setValue(docString);
res.setBody(commentBody);
final int authorPosition = docString.indexOf(AUTHOR_TAG);
if (authorPosition >= 0) {
final int authorStart = authorPosition + AUTHOR_TAG.length();
int authorEnd = docString.indexOf(NEW_LINE, authorStart);
res.setAuthor(docString.substring(authorStart, authorEnd));
}
final int versionPosition = docString.indexOf(VERSION_TAG);
if (versionPosition >= 0) {
final int versionStart = versionPosition + VERSION_TAG.length();
int versionEnd = docString.indexOf(NEW_LINE, versionStart);
res.setVersion(docString.substring(versionStart, versionEnd));
}
final int sincePosition = docString.indexOf(SINCE_TAG);
if (sincePosition >= 0) {
final int sinceStart = sincePosition + SINCE_TAG.length();
int sinceEnd = docString.indexOf(NEW_LINE, sinceStart);
if (sinceEnd >= 0) {
res.setSince(docString.substring(sinceStart, sinceEnd));
} else {
res.setSince(docString.substring(sinceStart, docString.length()));
}
}
setPositions(res, commentStartPositon, currentPosition);
} else {
res = null;
}
return res;
}
/**
* Parses a {@link ModuleElementDocumentation}.
*
* @return the created {@link ModuleElementDocumentation} if any recognized, <code>null</code> otherwise
*/
protected ModuleElementDocumentation parseModuleElementDocumentation() {
final ModuleElementDocumentation res;
if (text.startsWith(DOCUMENTATION_START, currentPosition)) {
final int commentStartPositon = currentPosition;
currentPosition += DOCUMENTATION_START.length();
final int startPosition = currentPosition;
int endPosition = getNext(DOCUMENTATION_END);
if (endPosition < 0) {
endPosition = text.length();
currentPosition = endPosition;
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createErrorModuleElementDocumentation();
((ErrorModuleElementDocumentation)res).setMissingEndHeader(endPosition);
errors.add((Error)res);
} else {
currentPosition = endPosition + DOCUMENTATION_END.length();
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createModuleElementDocumentation();
}
final String docString = text.substring(startPosition, endPosition);
final CommentBody commentBody = AcceleoPackage.eINSTANCE.getAcceleoFactory().createCommentBody();
setPositions(commentBody, currentPosition, endPosition);
commentBody.setValue(docString);
res.setBody(commentBody);
int paramPosition = docString.indexOf(PARAM_TAG);
while (paramPosition >= 0) {
final ParameterDocumentation paramDoc = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createParameterDocumentation();
final int paramStart = paramPosition + PARAM_TAG.length();
int paramEnd = docString.indexOf(NEW_LINE, paramStart);
if (paramEnd < 0) {
paramPosition = -1;
paramEnd = docString.length();
} else {
paramPosition = docString.indexOf(PARAM_TAG, paramEnd);
}
setPositions(paramDoc, paramStart + startPosition, paramEnd + startPosition);
final CommentBody paramBody = AcceleoPackage.eINSTANCE.getAcceleoFactory()
.createCommentBody();
paramBody.setValue(docString.substring(paramStart, paramEnd));
setPositions(paramBody, paramStart + startPosition, paramEnd + startPosition);
paramDoc.setBody(paramBody);
res.getParameterDocumentation().add(paramDoc);
}
setPositions(res, commentStartPositon, currentPosition);
} else {
res = null;
}
return res;
}
/**
* Parses a {@link Module}.
*
* @param comments
* the {@link List} of {@link Comment}
* @return the created {@link Module}
*/
protected Module parseModule(List<Comment> comments) {
final Module res;
if (text.startsWith(MODULE_HEADER_START, currentPosition)) {
// Start of the whole module, including comments and documentation that may be before the
// '[module' declaration.
final int startPosition = Stream.concat(Stream.of(currentPosition), comments.stream().map(
positions::getStartPositions)).min(Integer::compareTo).get();
// Start of the header of the module, i.e. where the '[module' declaration is located.
final int startHeaderPosition = currentPosition;
currentPosition += MODULE_HEADER_START.length();
skipSpaces();
final int identifierStartPosition = currentPosition;
final String name = parseIdentifier();
final int identifierEndPosition = currentPosition;
skipSpaces();
final int missingOpenParenthesis = readMissingString(OPEN_PARENTHESIS);
skipSpaces();
final List<Metamodel> metamodels = new ArrayList<Metamodel>();
Metamodel metamodel = parseMetamodel();
int missingEPackage = -1;
if (metamodel == null) {
missingEPackage = currentPosition;
}
while (metamodel != null) {
metamodels.add(metamodel);
skipSpaces();
if (readString(COMMA)) {
skipSpaces();
metamodel = parseMetamodel();
if (metamodel == null) {
missingEPackage = currentPosition;
} else {
missingEPackage = -1;
}
} else {
break;
}
}
skipSpaces();
final int missingCloseParenthesis = readMissingString(CLOSE_PARENTHESIS);
skipSpaces();
final ModuleReference extendedModule;
if (readString(EXTENDS)) {
skipSpaces();
extendedModule = parseModuleReference();
skipSpaces();
} else {
extendedModule = null;
}
final int missingEndHeader = readMissingString(MODULE_HEADER_END);
final int endHeaderPosition = currentPosition;
skipSpaces();
final List<Import> imports = new ArrayList<Import>();
Import imported = parseImport();
while (imported != null) {
imports.add(imported);
skipSpaces();
imported = parseImport();
}
final List<ModuleElement> moduleElements = parseModuleElements();
final int endPosition = currentPosition;
final boolean missingParenthesis = missingOpenParenthesis != -1 || missingCloseParenthesis != -1;
if (missingParenthesis || missingEPackage != -1 || missingEndHeader != -1) {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createErrorModule();
((ErrorModule)res).setMissingOpenParenthesis(missingOpenParenthesis);
((ErrorModule)res).setMissingEPackage(missingEPackage);
((ErrorModule)res).setMissingCloseParenthesis(missingCloseParenthesis);
((ErrorModule)res).setMissingEndHeader(missingEndHeader);
errors.add((ErrorModule)res);
} else {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createModule();
}
setIdentifierPositions(res, identifierStartPosition, identifierEndPosition);
setPositions(res, startPosition, endPosition);
res.setStartHeaderPosition(startHeaderPosition);
res.setEndHeaderPosition(endHeaderPosition);
res.getModuleElements().addAll(comments);
res.setDocumentation(getLastDocumentation(comments));
res.setName(name);
res.getMetamodels().addAll(metamodels);
res.getImports().addAll(imports);
res.setExtends(extendedModule);
res.getModuleElements().addAll(moduleElements);
} else {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createErrorModule();
errors.add((ErrorModule)res);
}
return res;
}
/**
* Parses an import {@link ModuleReference}.
*
* @return the create {@link ModuleReference} if any is recognized, <code>null</code> otherwise
*/
protected Import parseImport() {
final Import res;
if (text.startsWith(IMPORT_START, currentPosition)) {
final int startPosition = currentPosition;
currentPosition += IMPORT_START.length();
skipSpaces();
final ModuleReference moduleReference = parseModuleReference();
skipSpaces();
final int missingEnd = readMissingString(IMPORT_END);
if (missingEnd != -1) {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createErrorImport();
((ErrorImport)res).setMissingEnd(missingEnd);
errors.add((ErrorImport)res);
} else {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createImport();
}
res.setModule(moduleReference);
setPositions(res, startPosition, currentPosition);
} else {
res = null;
}
return res;
}
/**
* Parses a {@link ModuleReference}.
*
* @return the recognized {@link ModuleReference} if any, <code>null</code> otherwise
*/
protected ModuleReference parseModuleReference() {
final ModuleReference res;
final int startPosition = currentPosition;
final String moduleQualifiedName = parseModuleQualifiedName();
if (moduleQualifiedName == null) {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createErrorModuleReference();
errors.add((ErrorModuleReference)res);
} else {
res = AcceleoPackage.eINSTANCE.getAcceleoFactory().createModuleReference();
}
res.setQualifiedName(moduleQualifiedName);
setPositions(res, startPosition, currentPosition);
return res;
}
/**
* Parses module qualified name.
*
* @return the recognized module qualified name if any, <code>null</code> otherwise
*/
protected String parseModuleQualifiedName() {
final StringBuilder builder = new StringBuilder();
String segment = parseIdentifier();
while (segment != null) {
builder.append(segment);
if (readString(QUALIFIER_SEPARATOR)) {
builder.append(QUALIFIER_SEPARATOR);
segment = parseIdentifier();
} else {
break;
}
}
final String res;
if (builder.length() != 0) {
res = builder.toString();
} else {
res = null;
}
return res;
}
/**
* Gets the last {@link Documentation} from the given {@link List} of {@link Comment}.
*
* @param comments
* the {@link List} of {@link Comment}
* @return the last {@link Documentation} from the given {@link List} of {@link Comment}
*/
protected Documentation getLastDocumentation(List<Comment> comments) {
for (int i = comments.size() - 1; i >= 0; i--) {