public void testGetToken() { String data = "string: abcd"; StreamReader reader = new StreamReader(data); Scanner scanner = new ScannerImpl(reader); Mark dummy = new Mark("dummy", 0, 0, 0, "", 0); LinkedList<Token> etalonTokens = new LinkedList<Token>(); etalonTokens.add(new StreamStartToken(dummy, dummy)); etalonTokens.add(new BlockMappingStartToken(dummy, dummy)); etalonTokens.add(new KeyToken(dummy, dummy)); etalonTokens.add(new ScalarToken("string", true, dummy, dummy, (char) 0)); etalonTokens.add(new ValueToken(dummy, dummy)); etalonTokens.add(new ScalarToken("abcd", true, dummy, dummy, (char) 0)); etalonTokens.add(new BlockEndToken(dummy, dummy)); etalonTokens.add(new StreamEndToken(dummy, dummy)); while (!etalonTokens.isEmpty() && scanner.checkToken(etalonTokens.get(0).getTokenId())) { assertEquals(etalonTokens.removeFirst(), scanner.getToken()); } assertFalse("Must contain no more tokens: " + scanner.getToken(), scanner.checkToken(new Token.ID[0])); }
/** * * Handle implicitly ending multiple levels of block nodes by decreased * indentation. This function becomes important on lines 4 and 7 of this * example: * * <pre> * 1) book one: * 2) part one: * 3) chapter one * 4) part two: * 5) chapter one * 6) chapter two * 7) book two: * </pre> * * In flow context, tokens should respect indentation. Actually the * condition should be `self.indent >= column` according to the spec. But * this condition will prohibit intuitively correct constructions such as * key : { } </pre> */ private void unwindIndent(int col) { // In the flow context, indentation is ignored. We make the scanner less // restrictive then specification requires. if (this.flowLevel != 0) { return; } // In block context, we may need to issue the BLOCK-END tokens. while (this.indent > col) { Mark mark = reader.getMark(); this.indent = this.indents.pop(); this.tokens.add(new BlockEndToken(mark, mark)); } }
/** * * Handle implicitly ending multiple levels of block nodes by decreased indentation. This function becomes important on lines 4 and 7 * of this example: * * <pre> * 1) book one: * 2) part one: * 3) chapter one * 4) part two: * 5) chapter one * 6) chapter two * 7) book two: * </pre> * * In flow context, tokens should respect indentation. Actually the condition should be `self.indent >= column` according to the * spec. But this condition will prohibit intuitively correct constructions such as key : { } </pre> */ private void unwindIndent(final int col) { // In the flow context, indentation is ignored. We make the scanner less // restrictive then specification requires. if (this.flowLevel != 0) { return; } // In block context, we may need to issue the BLOCK-END tokens. while (this.indent > col) { Mark mark = reader.getMark(); this.indent = this.indents.pop(); this.tokens.add(new BlockEndToken(mark, mark)); } }