@SuppressWarnings("rawtypes") private Token accept(Class clazz) { if (this.peek().getClass().equals(clazz)) { return lexer.advance(); } return null; }
private List<CaseConditionNode> whenBlock() { expect(Indent.class); List<CaseConditionNode> caseConditionalNodes = new LinkedList<CaseConditionNode>(); while (!(peek() instanceof Outdent) && !(peek() instanceof Eos)) { if (peek() instanceof Newline) { advance(); } else { caseConditionalNodes.add(this.parseCaseCondition()); } } if (peek() instanceof Outdent) { expect(Outdent.class); } return caseConditionalNodes; }
@SuppressWarnings("rawtypes") private Token expect(Class expectedTokenClass) { Token t = this.peek(); if (t.getClass().equals(expectedTokenClass)) { return advance(); } else { throw new JadeParserException(filename, lexer.getLineno(), templateLoader, expectedTokenClass, t.getClass()); } }
public Node parse() { BlockNode block = new BlockNode(); block.setLineNumber(lexer.getLineno()); block.setFileName(filename); while (!(peek() instanceof Eos)) { if (peek() instanceof Newline) { advance(); } else { Node expr = parseExpr(); if (expr != null) { block.push(expr); } } } if (extending != null) { getContexts().push(extending); Node rootNode = extending.parse(); getContexts().pop(); // hoist mixins Set<String> keySet = this.mixins.keySet(); for (String name : keySet) { rootNode.getNodes().push(this.mixins.get(name)); } return rootNode; } return block; }
private Node parseTextBlock() { BlockNode blockNode = new BlockNode(); blockNode.setLineNumber(line()); blockNode.setFileName(filename); Token body = peek(); if(!(body instanceof PipelessText)){ return null; } this.advance(); ArrayList<String> values = body.getValues(); Node[] textNodes = {}; for (String value : values) { textNodes = ArrayUtils.addAll(textNodes,parseInlineTagsInText(value)); } blockNode.setNodes(new LinkedList<Node>(Arrays.asList(textNodes))); return blockNode; }
private Node parseBlockExpansion(){ if (this.peek() instanceof Colon) { this.advance(); BlockNode blockNode = new BlockNode(); blockNode.push(this.parseExpr()); return blockNode; } else { return this.block(); } }
private Node parseMixin() { Mixin mixinToken = (Mixin) expect(Mixin.class); MixinNode node = new MixinNode(); node.setName(mixinToken.getValue()); node.setLineNumber(mixinToken.getLineNumber()); node.setFileName(filename); if (StringUtils.isNotBlank(mixinToken.getArguments())) { node.setArguments(mixinToken.getArguments()); } List<String> args = node.getArguments(); String rest; if (args.size() > 0) { Matcher matcher = Pattern.compile("^\\.\\.\\.").matcher(args.get(args.size() - 1).trim()); if (matcher.find(0)) { rest = args.remove(args.size() - 1).trim().replaceAll("^\\.\\.\\.", ""); node.setRest(rest); } } if (peek() instanceof Indent) { this.inMixin++; node.setBlock(block()); node.setCall(false); this.mixins.put(mixinToken.getValue(),node); this.inMixin--; return node; }else{ node.setCall(true); return node; } }
private Node parseCase() { String val = expect(CaseToken.class).getValue(); Node node = new CaseNode(); node.setValue(val); node.setLineNumber(line()); Node block = new BlockNode(); block.setLineNumber(line()); block.setFileName(filename); expect(Indent.class); while (!(peek() instanceof Outdent)) { if (peek() instanceof Comment) { advance(); } else if (peek() instanceof Newline) { advance(); } else if (peek() instanceof When) { block.push(parseWhen()); } else if (peek() instanceof Default) { block.push(parseDefault()); } else { throw new JadeParserException(filename,lexer.getLineno(),templateLoader,"Unexpected token \"" + this.peek() + "\", expected \"when\", \"default\" or \"newline\""); } } expect(Outdent.class); node.setBlock(block); return node; } /**
private Node blockExpansion() { if (peek() instanceof Colon) { Token token = expect(Colon.class); Colon colon = (Colon) token; BlockNode block = new BlockNode(); block.setLineNumber(colon.getLineNumber()); block.setFileName(filename); block.getNodes().add(parseExpr()); return block; } return block(); }
/** * when */ private Node parseWhen(){ String val = this.expect(When.class).getValue(); CaseNode.When when = new CaseNode.When(); when.setValue(val); if (!(this.peek() instanceof Newline)) { when.setBlock(this.parseBlockExpansion()); } return when; }
/** * block code */ private Node parseBlockCode(){ Token tok = this.expect(BlockCode.class); ExpressionNode node; Token body = this.peek(); String text; if (body instanceof PipelessText) { this.advance(); text = StringUtils.join(body.getValues(),"\n"); } else { text = ""; } node = new ExpressionNode(); node.setValue(text); node.setLineNumber(tok.getLineNumber()); return node; }
private BlockNode block() { BlockNode block = new BlockNode(); block.setLineNumber(lexer.getLineno()); block.setFileName(filename); expect(Indent.class); while (!(peek() instanceof Outdent)) { if (peek() instanceof Newline) { advance(); } else { Node parseExpr = this.parseExpr(); parseExpr.setFileName(filename); if (parseExpr != null) { block.push(parseExpr); } } } expect(Outdent.class); return block; }
if (peek() instanceof Indent) { blockNode = block(); } else {
conditions.add(main); while (peek() instanceof ElseIf) { ElseIf token = (ElseIf) expect(ElseIf.class); IfConditionNode elseIf = new IfConditionNode(token.getValue(), token.getLineNumber()); if (peek() instanceof Else) { Else token = (Else) expect(Else.class); IfConditionNode elseNode = new IfConditionNode(null, token.getLineNumber());
Token incomingToken = peek(); if (incomingToken instanceof CssId) { Token tok = advance(); } else if (incomingToken instanceof AttributeList) { if (seenAttrs) { throw new JadeParserException(filename,line(),templateLoader,this.filename + ", line " + this.peek().getLineNumber() + ":\nYou should not have jade tags with multiple attributes."); if (peek() instanceof Dot) { dot = true; tagNode.setTextOnly(true); if (peek() instanceof Text) { tagNode.getBlock().push(parseText()); } else if (peek() instanceof Expression) { tagNode.setCodeNode(parseCode()); } else if (peek() instanceof Colon) { Token next = advance(); BlockNode block = new BlockNode(); while (peek() instanceof Newline) { advance(); block = new BlockNode(); tagNode.setBlock(block); }else if(peek() instanceof Indent){ BlockNode block = block();
contexts.pop(); ast.setFileName(path); if (peek() instanceof Indent && ast != null) { ((BlockNode) ast).getIncludeBlock().push(block());
private CaseConditionNode parseCaseCondition() { CaseConditionNode node = new CaseConditionNode(); Token token = null; if (peek() instanceof When) { token = expect(When.class); } else { token = expect(Default.class); node.setDefault(true); } node.setLineNumber(token.getLineNumber()); node.setFileName(filename); node.setValue(token.getValue()); node.setBlock(blockExpansion()); return node; }
private Node parseCode() { Token token = expect(Expression.class); Expression expressionToken = (Expression) token; ExpressionNode codeNode = new ExpressionNode(); codeNode.setValue(expressionToken.getValue()); codeNode.setBuffer(expressionToken.isBuffer()); codeNode.setEscape(expressionToken.isEscape()); codeNode.setLineNumber(expressionToken.getLineNumber()); codeNode.setFileName(filename); boolean block = false; // int i = 1; // while (lookahead(i) != null && lookahead(i) instanceof Newline) // ++i; block = peek() instanceof Indent; if (block) { codeNode.setBlock((BlockNode) block()); } return codeNode; }
private Node parseEach() { Token token = expect(Each.class); Each eachToken = (Each) token; EachNode node = new EachNode(); node.setValue(eachToken.getValue()); node.setKey(eachToken.getKey()); node.setCode(eachToken.getCode()); node.setLineNumber(eachToken.getLineNumber()); node.setFileName(filename); node.setBlock(block()); if (peek() instanceof Else) { advance(); node.setElseNode(block()); } return node; }
private Node parseExpr() { Token token = peek(); if (token instanceof Tag) { return parseTag();