|
31 | 31 | */ |
32 | 32 | package com.jme3.gde.glsl.highlighter.lexer; |
33 | 33 |
|
| 34 | +import com.jme3.gde.glsl.highlighter.util.Trie; |
34 | 35 | import java.util.ArrayList; |
35 | 36 | import java.util.List; |
36 | 37 |
|
|
40 | 41 | * |
41 | 42 | * @author grizeldi |
42 | 43 | */ |
43 | | -class GlslKeywordLibrary { |
44 | | - |
| 44 | +final class GlslKeywordLibrary { |
| 45 | + |
45 | 46 | public enum KeywordType { |
46 | 47 | KEYWORD, BUILTIN_FUNCTION, BUILTIN_VARIABLE, BASIC_TYPE, UNFINISHED; |
47 | 48 | } |
48 | | - private static final List<String> keywords = new ArrayList<>(), |
49 | | - builtinFunctions = new ArrayList<>(), |
50 | | - builtinVariables = new ArrayList<>(), |
51 | | - basicTypes = new ArrayList<>(); |
| 49 | + |
| 50 | + private static final Trie keywordsTrie = new Trie(); |
| 51 | + private static final Trie builtinFunctionsTrie = new Trie(); |
| 52 | + private static final Trie builtinVariablesTrie = new Trie(); |
| 53 | + private static final Trie basicTypesTrie = new Trie(); |
52 | 54 |
|
53 | 55 | static { |
54 | 56 | //keywords |
| 57 | + List<String> keywords = new ArrayList<>(); |
55 | 58 | keywords.add("attribute"); |
56 | 59 | keywords.add("const"); |
57 | 60 | keywords.add("uniform"); |
@@ -93,6 +96,7 @@ public enum KeywordType { |
93 | 96 | keywords.add("discard"); |
94 | 97 | keywords.add("return"); |
95 | 98 | //primitives and other types |
| 99 | + List<String> basicTypes = new ArrayList<>(); |
96 | 100 | basicTypes.add("float"); |
97 | 101 | basicTypes.add("double"); |
98 | 102 | basicTypes.add("int"); |
@@ -217,6 +221,7 @@ public enum KeywordType { |
217 | 221 | basicTypes.add("struct"); |
218 | 222 | //builtin variables |
219 | 223 | //compute shaders |
| 224 | + List<String> builtinVariables = new ArrayList<>(); |
220 | 225 | builtinVariables.add("gl_NumWorkGroups"); |
221 | 226 | builtinVariables.add("gl_WorkGroupSize"); |
222 | 227 | builtinVariables.add("gl_WorkGroupID"); |
@@ -291,6 +296,7 @@ public enum KeywordType { |
291 | 296 | builtinVariables.add("g_LightColor"); |
292 | 297 | builtinVariables.add("g_AmbientLightColor"); |
293 | 298 | //builtin functions |
| 299 | + List<String> builtinFunctions = new ArrayList<>(); |
294 | 300 | builtinFunctions.add("radians"); |
295 | 301 | builtinFunctions.add("degrees"); |
296 | 302 | builtinFunctions.add("sin"); |
@@ -466,51 +472,45 @@ public enum KeywordType { |
466 | 472 | builtinFunctions.add("memoryBarrierShared"); |
467 | 473 | builtinFunctions.add("memoryBarrierImage"); |
468 | 474 | builtinFunctions.add("groupMemoryBarrier"); |
| 475 | + |
| 476 | + // Create the search tries |
| 477 | + for(String keyword : keywords) { |
| 478 | + keywordsTrie.insert(keyword); |
| 479 | + } |
| 480 | + for(String keyword : builtinFunctions) { |
| 481 | + builtinFunctionsTrie.insert(keyword); |
| 482 | + } |
| 483 | + for(String keyword : builtinVariables) { |
| 484 | + builtinVariablesTrie.insert(keyword); |
| 485 | + } |
| 486 | + for(String keyword : basicTypes) { |
| 487 | + basicTypesTrie.insert(keyword); |
| 488 | + } |
469 | 489 | } |
470 | 490 |
|
471 | 491 | public static KeywordType lookup(String s) { |
472 | 492 | KeywordType returnType = null; |
473 | | - for (String primitive : basicTypes) { |
474 | | - if (primitive.startsWith(s)) { |
475 | | - if (primitive.equals(s)) { |
476 | | - returnType = KeywordType.BASIC_TYPE; |
477 | | - break; |
478 | | - } else { |
479 | | - returnType = KeywordType.UNFINISHED; |
480 | | - } |
481 | | - } |
| 493 | + returnType = lookup(s, returnType, KeywordType.BASIC_TYPE, basicTypesTrie); |
| 494 | + returnType = lookup(s, returnType, KeywordType.BUILTIN_VARIABLE, builtinVariablesTrie); |
| 495 | + if (returnType == KeywordType.UNFINISHED || returnType == null) { |
| 496 | + returnType = lookup(s, returnType, KeywordType.BUILTIN_FUNCTION, builtinFunctionsTrie); |
482 | 497 | } |
483 | | - for (String var : builtinVariables) { |
484 | | - if (var.startsWith(s)) { |
485 | | - if (var.equals(s)) { |
486 | | - returnType = KeywordType.BUILTIN_VARIABLE; |
487 | | - break; |
488 | | - } else { |
489 | | - returnType = KeywordType.UNFINISHED; |
490 | | - } |
491 | | - } |
| 498 | + if (returnType == KeywordType.UNFINISHED || returnType == null) { |
| 499 | + returnType = lookup(s, returnType, KeywordType.KEYWORD, keywordsTrie); |
492 | 500 | } |
493 | | - for (String func : builtinFunctions) { |
494 | | - if (func.startsWith(s) && (returnType == KeywordType.UNFINISHED || returnType == null)) { |
495 | | - if (func.equals(s)) { |
496 | | - returnType = KeywordType.BUILTIN_FUNCTION; |
497 | | - break; |
498 | | - } else { |
499 | | - returnType = KeywordType.UNFINISHED; |
500 | | - } |
501 | | - } |
| 501 | + |
| 502 | + return returnType; |
| 503 | + } |
| 504 | + |
| 505 | + private static KeywordType lookup(String s, KeywordType currentType, KeywordType matchType, Trie searchTrie) { |
| 506 | + Trie.MatchType match = searchTrie.search(s); |
| 507 | + if (match == Trie.MatchType.FULL_MATCH) { |
| 508 | + return matchType; |
502 | 509 | } |
503 | | - for (String keyword : keywords) { |
504 | | - if (keyword.startsWith(s) && (returnType == KeywordType.UNFINISHED || returnType == null)) { |
505 | | - if (keyword.equals(s)) { |
506 | | - returnType = KeywordType.KEYWORD; |
507 | | - break; |
508 | | - } else { |
509 | | - returnType = KeywordType.UNFINISHED; |
510 | | - } |
511 | | - } |
| 510 | + if (match == Trie.MatchType.PARTIAL_MATCH) { |
| 511 | + return KeywordType.UNFINISHED; |
512 | 512 | } |
513 | 513 |
|
514 | | - return returnType; |
| 514 | + return currentType; |
515 | 515 | } |
516 | 516 | } |
0 commit comments