From Text File To Tokens

Rather than hardcoding a long list of instances of org.netbeans.api.lexer.TokenId in my implementation of org.netbeans.spi.lexer.LanguageHierarchy, I have a simpler solution, now.
InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream("org/demo/tokens.txt");
Properties props = new Properties();
props.load(inputStream);
Enumeration e = props.propertyNames();
tokens = new ArrayList<MyTokenId>();
while (e.hasMoreElements()) {
    String key = e.nextElement().toString();
    int value = Integer.parseInt(props.getProperty(key));

While iterating through the enumeration, create a new Token from each subsequent key/value pair in the file, and add it to the list of Tokens.

The "org/demo/tokens.txt" file referred to above has a long list of key/value pairs, such as these:

ABSTRACT=4
ACTIVATE=5
ADDRESS_OF=6
AMP=7
AND=8
ARGUMENT_LIST=9
ARRAY_ELEMENT_SELECTION=10
ARROW=11
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today