PostLexer implements TokenSource { @Override public Token nextToken() { LOGGER.trace("Entering nextToken()"); if (!heap.isEmpty()) { return heap.poll(); } while (true) { getNextToken(); switch (currToken.getType()) { case PreprocessorParser.AMPIF: preproIf(); return heap.poll(); case PreprocessorParser.AMPTHEN: throwMessage("Unexpected &THEN"); break; case PreprocessorParser.AMPELSEIF: heap.offer(currToken); preproElseif(); return heap.poll(); case PreprocessorParser.AMPELSE: heap.offer(currToken); preproElse(); return heap.poll(); case PreprocessorParser.AMPENDIF: preproEndif(); return currToken; default: return currToken; } } } PostLexer(ABLLexer parent, Lexer lexer); @Override Token nextToken(); @Override int getLine(); @Override int getCharPositionInLine(); @Override CharStream getInputStream(); @Override String getSourceName(); @Override void setTokenFactory(TokenFactory<?> factory); @Override TokenFactory<?> getTokenFactory();  }
@Test public void testPostLexer01Init() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "postlexer01.p"), session); TokenSource src = unit.preprocess(); ProToken tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.AMPIF); assertEquals(tok.getChannel(), ProToken.PREPROCESSOR_CHANNEL); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PREPROEXPR_TRUE); assertEquals(tok.getChannel(), ProToken.PREPROCESSOR_CHANNEL); assertEquals(tok.getText(), "TRUE || INTEGER(00) == 0 && (2 + 3) == 5"); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.AMPTHEN); assertEquals(tok.getChannel(), ProToken.PREPROCESSOR_CHANNEL); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.WS); assertEquals(tok.getChannel(), 1); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.AMPSCOPEDDEFINE); assertEquals(tok.getChannel(), 2); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.WS); assertEquals(tok.getChannel(), 1); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.AMPENDIF); assertEquals(tok.getChannel(), ProToken.PREPROCESSOR_CHANNEL); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.WS); assertEquals(tok.getChannel(), 1); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.QSTRING); assertEquals(tok.getText(), "\"zz\""); }
@Test public void testPreproErrorMessages01() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer06.p"), session); try { TokenSource src = unit.preprocess(); while (src.nextToken().getType() != Token.EOF) { } } catch (ProparseRuntimeException caught) { assertTrue(caught.getMessage().replace('\\', '/').startsWith("File '" + SRC_DIR + "/lexer06.p'")); assertTrue(caught.getMessage().endsWith("Unexpected &THEN")); return; } catch (Exception caught) { fail("Unwanted exception..."); } fail("No exception found"); }
@Test public void testPreproErrorMessages02() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer07.p"), session); try { TokenSource src = unit.preprocess(); while (src.nextToken().getType() != Token.EOF) { } } catch (ProparseRuntimeException caught) { assertTrue(caught.getMessage().replace('\\', '/').startsWith("File '" + SRC_DIR + "/lexer07.p'")); assertTrue(caught.getMessage().endsWith("Unexpected end of input after &IF or &ELSEIF")); return; } catch (Exception caught) { fail("Unwanted exception..."); } fail("No exception found"); }
@Test public void testPreproErrorMessages03() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer08.p"), session); try { TokenSource src = unit.preprocess(); while (src.nextToken().getType() != Token.EOF) { } } catch (ProparseRuntimeException caught) { assertTrue(caught.getMessage().replace('\\', '/').startsWith("File '" + SRC_DIR + "/lexer08.p'")); assertTrue( caught.getMessage().endsWith("Unexpected end of input when consuming discarded &IF/&ELSEIF/&ELSE text")); return; } catch (Exception caught) { fail("Unwanted exception..."); } fail("No exception found"); }
@Test public void testPreproErrorMessages04() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer09.p"), session); try { TokenSource src = unit.preprocess(); while (src.nextToken().getType() != Token.EOF) { } } catch (ProparseRuntimeException caught) { String msg = caught.getMessage().replace('\\', '/'); assertTrue(msg.startsWith("File '" + SRC_DIR + "/lexer09.p' - Current position '"), msg); assertTrue(msg.endsWith("src/test/resources/data/lexer/lexer09.i':2 - Unexpected &THEN"), msg); return; } catch (Exception caught) { fail("Unwanted exception..."); } fail("No exception found"); }
@Test public void testAnalyzeSuspendIncludeFile() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer10.p"), session); TokenSource stream = unit.preprocess(); ProToken tok = nextToken(stream, ABLNodeType.MESSAGE); assertNull(tok.getAnalyzeSuspend()); assertTrue(tok.isEditableInAB()); tok = nextToken(stream, ABLNodeType.MESSAGE); assertNotNull(tok.getAnalyzeSuspend()); assertFalse(tok.getAnalyzeSuspend().isEmpty()); assertTrue(tok.isEditableInAB()); tok = nextToken(stream, ABLNodeType.MESSAGE); assertNotNull(tok.getAnalyzeSuspend()); assertFalse(tok.getAnalyzeSuspend().isEmpty()); assertFalse(tok.isEditableInAB()); tok = nextToken(stream, ABLNodeType.MESSAGE); assertNull(tok.getAnalyzeSuspend()); assertTrue(tok.isEditableInAB()); tok = nextToken(stream, ABLNodeType.MESSAGE); assertNotNull(tok.getAnalyzeSuspend()); assertTrue(tok.getAnalyzeSuspend().isEmpty()); assertFalse(tok.isEditableInAB()); tok = nextToken(stream, ABLNodeType.MESSAGE); assertNull(tok.getAnalyzeSuspend()); assertTrue(tok.isEditableInAB()); }
@Test(enabled = false) public void testMacroExpansion() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer12.p"), session); TokenSource stream = unit.preprocess(); ProToken tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.MESSAGE); assertTrue(tok.isMacroExpansion()); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.QSTRING); assertTrue(tok.isMacroExpansion()); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.PERIOD); assertTrue(tok.isMacroExpansion()); }
@Test public void testProparseDirectiveLexPhase() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer15.p"), session); TokenSource stream = unit.lex(); ProToken tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PROPARSEDIRECTIVE); assertEquals(tok.getChannel(), ProToken.PROPARSE_CHANNEL); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.WS); assertEquals(tok.getChannel(), Token.HIDDEN_CHANNEL); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.ID); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.EQUAL); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.NUMBER); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PERIOD); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PROPARSEDIRECTIVE); assertEquals(tok.getChannel(), ProToken.PROPARSE_CHANNEL); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.ID); assertEquals(tok.getText(), "customer.custnum"); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.EQUAL); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.NUMBER); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PERIOD); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PROPARSEDIRECTIVE); assertEquals(tok.getChannel(), ProToken.PROPARSE_CHANNEL); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.ID); assertEquals(tok.getText(), "sp2k.customer.custnum"); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.EQUAL); tok = (ProToken) stream.nextToken(); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.NUMBER); tok = (ProToken) stream.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PERIOD); }
@Test public void testHexNumbers() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer16.p"), session); TokenSource stream = unit.lex(); ProToken tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.MESSAGE); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "125"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "0x65"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "0X66"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "0xfb"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "0xab"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "-0x01"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.PERIOD); }
@Test public void testHexNumbers2() { ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer17.p"), session); TokenSource stream = unit.lex(); ProToken tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.MESSAGE); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.NUMBER); assertEquals(tok.getText(), "125"); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.WS); tok = (ProToken) stream.nextToken(); assertEquals(tok.getType(), Proparse.ID); assertEquals(tok.getText(), "0x2g8"); }
@Test public void testEndOfIncInIncludeParameter() { Injector injector = Guice.createInjector(new UnitTestWindowsModule()); RefactorSession session = injector.getInstance(RefactorSession.class); ParseUnit unit = new ParseUnit(new File(SRC_DIR, "lexer21.p"), session); TokenSource src = unit.preprocess(); ProToken tok = (ProToken) nextVisibleToken(src); assertEquals(tok.getNodeType(), ABLNodeType.FIND); tok = (ProToken) nextVisibleToken(src); assertEquals(tok.getNodeType(), ABLNodeType.ID); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.WS); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.COMMENT); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.WS); tok = (ProToken) src.nextToken(); assertEquals(tok.getNodeType(), ABLNodeType.PERIOD); }