X

Geertjan's Blog

  • August 11, 2013

From Text File To Tokens

Geertjan Wielenga
Product Manager
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

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.