public void setID(beaver.Symbol symbol) { if (symbol.value != null && !(symbol.value instanceof String)) throw new UnsupportedOperationException("setID is only valid for String lexemes"); tokenString_ID = (String) symbol.value; IDstart = symbol.getStart(); IDend = symbol.getEnd(); }
private void processPHPBlock(IParseNode root, int sourceLength) throws IOException, Exception // $codepro.audit.disable { Symbol startTag = getCurrentSymbol(); advance(); // finds the entire php block int start = getCurrentSymbol().getStart(); int end = start; short id = getCurrentSymbol().getId(); while (id != PHTMLTokens.PHP_END && id != PHTMLTokens.EOF) { end = getCurrentSymbol().getEnd(); advance(); id = getCurrentSymbol().getId(); } IParseNode result = getParseResult(IPHPConstants.CONTENT_TYPE_PHP, start, end); if (result != null) { Symbol endTag = getCurrentSymbol(); ParseNode phpNode = new ParseNode(IPHPConstants.CONTENT_TYPE_PHP); int endOffset; if ("?>".equals(endTag.value)) // $NON-NLS-1$ { endOffset = endTag.getEnd() + 1; } else { endOffset = endTag.getEnd(); } phpNode.setLocation(startTag.getStart(), Math.min(endOffset, sourceLength - 1)); root.addChild(phpNode); } }
/* * Constructs a Symbol from a line of text. * Format: * TYPE start_line start_col length [=value] * e.g. FOR 2 1 3 * IDENTIFIER 3 2 1 =x */ private static Symbol parseSymbol(String line) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { Matcher matcher = SYMBOL_PATTERN.matcher(line); if (!matcher.matches()) { fail("Invalid line: " + line); } String idString = matcher.group(1); short id = AspectsParser.Terminals.class .getField(idString) .getShort(null); // null since the field is static int lineNum = Integer.parseInt(matcher.group(2)); int colNum = Integer.parseInt(matcher.group(3)); String value = matcher.group(6); if (matcher.group(5) == null) { int length = Integer.parseInt(matcher.group(4)); return new Symbol(id, lineNum, colNum, length, value); } else { int endLine = Integer.parseInt(matcher.group(4)); int endCol = Integer.parseInt(matcher.group(5)); int startPos = Symbol.makePosition(lineNum, colNum); int endPos = Symbol.makePosition(endLine, endCol); return new Symbol(id, startPos, endPos, value); } }
/** * assertTokenType * * @param source * @param type * @throws Exception */ protected void assertTokenType(String source, JSTokenType type) { _scanner.setSource(source); try { Symbol token = _scanner.nextToken(); int id = token.getId(); int length = token.getEnd() - token.getStart() + 1; assertEquals("unexpected token type", type.getIndex(), id); assertEquals("token length does not match source length: " + source, source.length(), length); } catch (Throwable e) { fail(e.getMessage()); } }
/** * assertTokenTypes * * @param source * @param types * @throws Exception */ protected void assertTokenTypes(String source, JSTokenType... types) { _scanner.setSource(source); for (int i = 0; i < types.length; i++) { try { JSTokenType type = types[i]; Symbol token = _scanner.nextToken(); int id = token.getId(); assertEquals("in '" + source + "' at token index " + i, type.getIndex(), id); } catch (Throwable e) { fail(e.getMessage()); } } }
/* (non-Javadoc) * @see com.aptana.editor.json.parsing.ast.JSONTreeWalker#visit(com.aptana.editor.json.parsing.ast.JSONEntryNode) */ @Override public void visit(JSONEntryNode node) { JSONEntryFormatNode entry = new JSONEntryFormatNode(_document); IParseNode name = node.getFirstChild(); Symbol colon = node.getColon(); IParseNode value = node.getLastChild(); IFormatterTextNode nameText = createTextNode(_document, name.getStartingOffset(), name.getEndingOffset() + 1); IFormatterTextNode colonText = createTextNode(_document, colon.getStart(), colon.getEnd() + 1); // add children entry.addChild(nameText); entry.addChild(colonText); // push node, visit children, then remove from stack push(entry); this.visit(value); checkedPop(entry, node.getEndingOffset() + 1); }
/** * Resumes scanning until the next regular expression is matched, the end of input is encountered * or an I/O-Error occurs. * * @return the next token * @exception java.io.IOException if any I/O-Error occurs */ public Symbol nextToken() throws java.io.IOException, Scanner.Exception { int zzInput; int zzAction; // cached fields: int zzCurrentPosL; int zzMarkedPosL; int zzEndReadL = zzEndRead; char[] zzBufferL = zzBuffer; char[] zzCMapL = ZZ_CMAP; int[] zzTransL = ZZ_TRANS; int[] zzRowMapL = ZZ_ROWMAP; int[] zzAttrL = ZZ_ATTRIBUTE; while (true) { zzMarkedPosL = zzMarkedPos; boolean zzR = false; for (zzCurrentPosL = zzStartRead; zzCurrentPosL < zzMarkedPosL; zzCurrentPosL++) { switch (zzBufferL[zzCurrentPosL]) { case '\u000b': case '\u000c': case '\u0085': case '\u2028': case '\u2029': yyline++; yycolumn = 0; zzR = false; break; case '\r': yyline++; yycolumn = 0; zzR = true; break; case '\n': if (zzR) zzR = false; else { yyline++; yycolumn = 0; } break; default: zzR = false; yycolumn++; } } if (zzR) { // peek one character ahead if it is \n (if we have counted one line too much) boolean zzPeek; if (zzMarkedPosL < zzEndReadL) zzPeek = zzBufferL[zzMarkedPosL] == '\n'; else if (zzAtEOF) zzPeek = false; else { boolean eof = zzRefill(); zzEndReadL = zzEndRead; zzMarkedPosL = zzMarkedPos; zzBufferL = zzBuffer; if (eof) zzPeek = false; else zzPeek = zzBufferL[zzMarkedPosL] == '\n'; } if (zzPeek) yyline--; } zzAction = -1; zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL; zzState = ZZ_LEXSTATE[zzLexicalState]; zzForAction: { while (true) { if (zzCurrentPosL < zzEndReadL) zzInput = zzBufferL[zzCurrentPosL++]; else if (zzAtEOF) { zzInput = YYEOF; break zzForAction; } else { // store back cached positions zzCurrentPos = zzCurrentPosL; zzMarkedPos = zzMarkedPosL; boolean eof = zzRefill(); // get translated positions and possibly new buffer zzCurrentPosL = zzCurrentPos; zzMarkedPosL = zzMarkedPos; zzBufferL = zzBuffer; zzEndReadL = zzEndRead; if (eof) { zzInput = YYEOF; break zzForAction; } else { zzInput = zzBufferL[zzCurrentPosL++]; } } int zzNext = zzTransL[zzRowMapL[zzState] + zzCMapL[zzInput]]; if (zzNext == -1) break zzForAction; zzState = zzNext; int zzAttributes = zzAttrL[zzState]; if ((zzAttributes & 1) == 1) { zzAction = zzState; zzMarkedPosL = zzCurrentPosL; if ((zzAttributes & 8) == 8) break zzForAction; } } } // store back cached position zzMarkedPos = zzMarkedPosL; switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) { case 9: { /* ignore */ } case 41: break; case 10: { return newSymbol(Terminals.IDENT, yytext()); } case 42: break; case 37: { return newSymbol(Terminals.PACKAGE); } case 43: break; case 3: { yybegin(EOF_MLN_COMMENT); } case 44: break; case 33: { return newSymbol(Terminals.CLASS); } case 45: break; case 38: { return newSymbol(Terminals.NONASSOC); } case 46: break; case 16: { return newSymbol(Terminals.DOT); } case 47: break; case 4: { yybegin(EOF_EOL_COMMENT); } case 48: break; case 35: { return newSymbol(Terminals.IMPORT); } case 49: break; case 19: { return newSymbol(Terminals.PLUS); } case 50: break; case 5: { throw new Scanner.Exception( token_line + 1, token_column + 1, "end of file in Java code"); } case 51: break; case 18: { return newSymbol(Terminals.QUESTION); } case 52: break; case 6: { throw new Scanner.Exception(token_line + 1, token_column + 1, "end of file in comment"); } case 53: break; case 2: { yybegin(YYINITIAL); matched_text = null; throw new Scanner.Exception(token_line + 1, token_column + 1, "unterminated string"); } case 54: break; case 29: { return newSymbol(Terminals.INIT); } case 55: break; case 7: { yybegin(YYINITIAL); } case 56: break; case 1: { yybegin(EOF_CODE); } case 57: break; case 30: { return newSymbol(Terminals.LEFT); } case 58: break; case 34: { return newSymbol(Terminals.HEADER); } case 59: break; case 39: { return newSymbol(Terminals.TERMINALS); } case 60: break; case 11: { token_line = yyline; token_column = yycolumn; yybegin(TEXT); } case 61: break; case 23: { token_line = yyline; token_column = yycolumn; yybegin(CODE); } case 62: break; case 32: { return newSymbol(Terminals.RIGHT); } case 63: break; case 24: { token_line = yyline; token_column = yycolumn; yybegin(MLN_COMMENT); } case 64: break; case 17: { return newSymbol(Terminals.BAR); } case 65: break; case 28: { return newSymbol(Terminals.GOAL); } case 66: break; case 21: { matched_text = yytext(); } case 67: break; case 22: { yybegin(YYINITIAL); String txt = matched_text; matched_text = null; return new Symbol( Terminals.TEXT, Symbol.makePosition(token_line + 1, token_column + 1), Symbol.makePosition(yyline + 1, yycolumn + 1), txt); } case 68: break; case 31: { return newSymbol(Terminals.EMBED); } case 69: break; case 25: { token_line = yyline; token_column = yycolumn; yybegin(EOL_COMMENT); } case 70: break; case 27: { yybegin(YYINITIAL); return new Symbol( Terminals.CODE, Symbol.makePosition(token_line + 1, token_column + 1), Symbol.makePosition(yyline + 1, yycolumn + 3), matched_text); } case 71: break; case 20: { return newSymbol(Terminals.STAR); } case 72: break; case 26: { yypushback(2); matched_text = yytext(); yybegin(CODE_END); } case 73: break; case 13: { return newSymbol(Terminals.IS); } case 74: break; case 40: { return newSymbol(Terminals.IMPLEMENTS); } case 75: break; case 36: { return newSymbol(Terminals.TYPEOF); } case 76: break; case 14: { return newSymbol(Terminals.SEMI); } case 77: break; case 8: { throw new Scanner.Exception( yyline + 1, yycolumn + 1, "unrecognized character '" + yytext() + "'"); } case 78: break; case 12: { return newSymbol(Terminals.COMMA); } case 79: break; case 15: { return newSymbol(Terminals.AT); } case 80: break; default: if (zzInput == YYEOF && zzStartRead == zzCurrentPos) { zzAtEOF = true; { return newSymbol(Terminals.EOF, "end-of-file"); } } else { zzScanError(ZZ_NO_MATCH); } } } }
public int getFirstLine() { return Symbol.getLine(start_pos); }
/* Returns a pretty-printed version of a Symbol. */ protected static String getSymbolString(Symbol symbol) { return AspectsParser.Terminals.NAMES[symbol.getId()] + (symbol.value == null ? "" : "(" + symbol.value + ")"); }
/* Checks deep equality of two Symbols (complexity comes from giving nice error messages). */ public static void assertEquals(String msg, Symbol actual, Symbol expected) { final short expectedId = expected.getId(); final short actualId = actual.getId(); if (actualId != expectedId) { fail( msg + ": incorrect token type - " + "expected: " + expectedId + " (" + AspectsParser.Terminals.NAMES[expectedId] + ") " + "but was: " + actualId + " (" + AspectsParser.Terminals.NAMES[actualId] + ")"); } final String expectedValue = (String) expected.value; final String actualValue = ScannerTestTool.stringifyValue(actual.value); if (((actualValue == null || expectedValue == null) && (actualValue != expectedValue)) || (actualValue != null && !actualValue.equals(expectedValue))) { fail( msg + " - " + AspectsParser.Terminals.NAMES[actualId] + ": incorrect token value - " + "expected: " + expectedValue + " " + "but was: " + actualValue); } final int expectedStart = expected.getStart(); final int actualStart = actual.getStart(); if (actualStart != expectedStart) { fail( msg + " - " + getSymbolString(actual) + ": incorrect start position - " + "expected: line " + Symbol.getLine(expectedStart) + " col " + Symbol.getColumn(expectedStart) + " " + "but was: line " + Symbol.getLine(actualStart) + " col " + Symbol.getColumn(actualStart)); } final int expectedEnd = expected.getEnd(); final int actualEnd = actual.getEnd(); if (actualEnd != expectedEnd) { fail( msg + " - " + getSymbolString(actual) + ": incorrect end position - " + "expected: line " + Symbol.getLine(expectedEnd) + " col " + Symbol.getColumn(expectedEnd) + " " + "but was: line " + Symbol.getLine(actualEnd) + " col " + Symbol.getColumn(actualEnd)); } }