public class SyntaxTokenizer
extends java.lang.Object
To use a SyntaxTokenizer, construct one and then call its setText() method, supplying the Mathematica input you want tokenized. You then call getNextRecord() repeatedly to get SyntaxRecords, which tell you the type of syntax element and the length in characters.
This process is very fast. You can iterate through 100,000 characters of Mathematica code in a small fraction of a second
Here is some sample code that demonstrates how to use a SyntaxTokenizer:
String input = "some Mathematica code here";
SyntaxTokenizer tok = new SyntaxTokenizer();
tok.setText(input);
while(tok.hasMoreRecords()) {
SyntaxTokenizer.SyntaxRecord rec = tok.getNextRecord();
System.out.println("type: " + rec.type);
System.out.println("text: " + input.substring(rec.start, rec.start + rec.length));
}MathSessionPane| Modifier and Type | Class and Description |
|---|---|
class |
SyntaxTokenizer.SyntaxRecord
A simple class the encapsulates information about a syntax element.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
COMMENT
A syntax type that corresponds to a Mathematica comment.
|
static int |
NORMAL
A syntax type that consists of everything other than STRING, COMMENT, or SYMBOL.
|
static int |
STRING
A syntax type that corresponds to a literal string.
|
static int |
SYMBOL
A syntax type that corresponds to a Mathematica symbol.
|
| Constructor and Description |
|---|
SyntaxTokenizer() |
| Modifier and Type | Method and Description |
|---|---|
SyntaxTokenizer.SyntaxRecord |
getNextRecord()
Gets the next SyntaxRecord specifying the type of the element (SYMBOL, STRING, COMMENT or NORMAL),
its start position, and length.
|
boolean |
hasMoreRecords()
Returns true or false to indicate whether there are any more records left in the text
(i.e., whether we have come to the end of the input).
|
void |
reset()
Resets the state of the tokenizer so that the next call to getNextRecord() will retrieve
the first record in the text.
|
void |
setText(java.lang.String text)
Sets the Mathematica input text to tokenize.
|
public static final int NORMAL
public static final int STRING
public static final int COMMENT
public static final int SYMBOL
public void setText(java.lang.String text)
text - public void reset()
public SyntaxTokenizer.SyntaxRecord getNextRecord()
public boolean hasMoreRecords()
J/Link is Copyright (c) 1999-2017, Wolfram Research, Inc. All rights reserved.