本文整理汇总了Java中jdk.nashorn.internal.parser.TokenStream类的典型用法代码示例。如果您正苦于以下问题:Java TokenStream类的具体用法?Java TokenStream怎么用?Java TokenStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TokenStream类属于jdk.nashorn.internal.parser包,在下文中一共展示了TokenStream类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: dumpTokens
import jdk.nashorn.internal.parser.TokenStream; //导入依赖的package包/类
/**
* Dump a token stream to stdout
*
* TODO: most other bugging goes to stderr, change?
*
* @param source the source
* @param lexer the lexer
* @param stream the stream to dump
*/
public static void dumpTokens(final Source source, final Lexer lexer, final TokenStream stream) {
TokenType type;
int k = 0;
do {
while (k > stream.last()) {
// Get more tokens.
lexer.lexify();
}
final long token = stream.get(k);
type = Token.descType(token);
System.out.println("" + k + ": " + Token.toString(source, token, true));
k++;
} while(type != EOF);
}
示例2: getCommentsTokens
import jdk.nashorn.internal.parser.TokenStream; //导入依赖的package包/类
/**
* Extracts the comments tokens from a JavaScript source.
*
* @param aSource a source
* @return a list of comment tokens
*/
public static List<Long> getCommentsTokens(String aSource) {
TokenStream tokens = new TokenStream();
Lexer lexer = new Lexer(Source.sourceFor("", aSource), tokens);//NOI18N
long t;
TokenType tt = TokenType.EOL;
int i = 0;
List<Long> commentsTokens = new ArrayList<>();
while (tt != TokenType.EOF) {
// Get next token in nashorn's parser way
while (i > tokens.last()) {
if (tokens.isFull()) {
tokens.grow();
}
lexer.lexify();
}
t = tokens.get(i++);
tt = Token.descType(t);
if (tt == TokenType.COMMENT) {
commentsTokens.add(t);
}
}
return commentsTokens;
}
示例3: parseJs
import jdk.nashorn.internal.parser.TokenStream; //导入依赖的package包/类
public static ParsedJs parseJs(String aJsContent) {
Source source = Source.sourceFor("", aJsContent);//NOI18N
Options options = new Options(null);
ScriptEnvironment env = new ScriptEnvironment(options, null, null);
ErrorManager errors = new ErrorManager();
//
Map<Long, Long> prevComments = new HashMap<>();
Parser p = new Parser(env, source, errors) {
@Override
public FunctionNode parse(String scriptName, int startPos, int len, boolean allowPropertyFunction) {
prevComments.clear();
stream = new TokenStream() {
protected long prevToken;
@Override
public void put(long token) {
if (Token.descType(token) != TokenType.EOL) {
if (Token.descType(prevToken) == TokenType.COMMENT) {
prevComments.put(token, prevToken);
}
prevToken = token;
}
super.put(token);
}
};
lexer = new Lexer(source, stream, false);
// Set up first token (skips opening EOL.)
k = -1;
next();
// Begin parse.
try {
Method program = Parser.class.getDeclaredMethod("program", new Class[]{String.class, boolean.class});
program.setAccessible(true);
return (FunctionNode) program.invoke(this, new Object[]{scriptName, true});
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(Scripts.class.getName()).log(Level.WARNING, null, ex);
return null;
}
}
};
FunctionNode jsAst = p.parse();
return jsAst != null ? new ParsedJs(jsAst, prevComments) : null;
}