This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
/
LangPartitionScanner.java
96 lines (76 loc) · 3.54 KB
/
LangPartitionScanner.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
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* 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:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.core_text;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;
import static melnorme.utilbox.core.Assert.AssertNamespace.assertTrue;
import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.PatternRule;
import org.eclipse.jface.text.rules.Token;
import LANG_PROJECT_ID.ide.core_text.RuleBasedPartitionScannerExt;
import melnorme.lang.tooling.parser.lexer.IPredicateLexingRule;
import melnorme.utilbox.collections.ArrayList2;
/**
* A note about using scanner rules: Each rule should map to a unique partition type.
* This is because when partitioning with resuming, the partition type will be used to determine
* which rule to try to find the end of the partition.
* As such the same rule that was used to start the partition, should be used to find the end of it.
* The only way using multiple rules for the same partition type is ok, is when the rules all use the same terminator.
*/
public abstract class LangPartitionScanner extends RuleBasedPartitionScannerExt {
protected static final char NO_ESCAPE_CHAR = (char) -1;
public LangPartitionScanner() {
}
/* ----------------- ----------------- */
public class PredicateRule_Adapter extends DefaultPredicateRule {
protected final IPredicateLexingRule rule;
public PredicateRule_Adapter(String partitionTypeId, IPredicateLexingRule rule) {
super(new Token(partitionTypeId));
this.rule = assertNotNull(rule);
}
@Override
public IToken evaluate(ICharacterScanner scanner) {
assertTrue(scanner == LangPartitionScanner.this);
CharacterScanner_ReaderHelper readerHelper = new CharacterScanner_ReaderHelper(LangPartitionScanner.this);
if(rule.tryMatch(readerHelper)) {
return token;
} else {
return Token.UNDEFINED;
}
}
}
/* ----------------- ----------------- */
/***
* Add some partition rules common to C-style languages.
* All rules are optional, if an id is null, the rule will not be added.
*/
protected static void addStandardRules(ArrayList2<IPredicateRule> rules,
String lineCommentId, String blockCommentId,
String docLineCommentId, String docBlockCommentId,
String stringId) {
if(docLineCommentId != null) {
rules.add(new PatternRule("///", null, new Token(docLineCommentId), NO_ESCAPE_CHAR, true, true));
}
if(docBlockCommentId != null) {
rules.add(new PatternRule("/**", "*/", new Token(docBlockCommentId), NO_ESCAPE_CHAR, false, true));
}
if(lineCommentId != null) {
rules.add(new PatternRule("//", null, new Token(lineCommentId), NO_ESCAPE_CHAR, true, true));
}
if(blockCommentId != null) {
rules.add(new PatternRule("/*", "*/", new Token(blockCommentId), NO_ESCAPE_CHAR, false, true));
}
if(stringId != null) {
rules.add(new PatternRule("\"", "\"", new Token(stringId), '\\', false, true));
}
}
}