Beispiel #1
0
  private Node parseMixinInject() {
    Token token = expect(MixinInject.class);
    MixinInject mixinInjectToken = (MixinInject) token;
    MixinInjectNode node = new MixinInjectNode();
    node.setName(mixinInjectToken.getValue());
    node.setLineNumber(mixinInjectToken.getLineNumber());
    node.setFileName(filename);

    if (StringUtils.isNotBlank(mixinInjectToken.getArguments())) {
      node.setArguments(mixinInjectToken.getArguments());
    }

    while (true) {
      Token incomingToken = peek();
      if (incomingToken instanceof CssId) {
        Token tok = nextToken();
        node.addAttribute("id", tok.getValue());
      } else if (incomingToken instanceof CssClass) {
        Token tok = nextToken();
        node.addAttribute("class", tok.getValue());
      } else if (incomingToken instanceof Attribute) {
        Attribute tok = (Attribute) nextToken();
        node.addAttributes(tok.getAttributes());
      } else {
        break;
      }
    }

    if (peek() instanceof Text) {
      node.setBlock(parseText());
    } else if (peek() instanceof Indent) {
      node.setBlock(block());
    }
    return node;
  }
Beispiel #2
0
 private Node parseText() {
   Token token = expect(Text.class);
   Node node = new TextNode();
   node.setValue(token.getValue());
   node.setLineNumber(token.getLineNumber());
   node.setFileName(filename);
   return node;
 }
Beispiel #3
0
 @SuppressWarnings("rawtypes")
 private Token expect(Class expectedTokenClass) {
   Token t = this.peek();
   if (t.getClass().equals(expectedTokenClass)) {
     return nextToken();
   } else {
     throw new JadeParserException(
         filename, lexer.getLineno(), templateLoader, expectedTokenClass, t.getClass());
   }
 }
Beispiel #4
0
 private Node parseAssignment() {
   Token token = expect(Assignment.class);
   Token assignmentToken = (Assignment) token;
   Node node = new AssigmentNode();
   node.setName(assignmentToken.getName());
   node.setValue(assignmentToken.getValue());
   node.setLineNumber(assignmentToken.getLineNumber());
   node.setFileName(filename);
   return node;
 }
Beispiel #5
0
 private Node 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;
 }
Beispiel #6
0
  private Node parseComment() {
    Token token = expect(Comment.class);

    CommentNode node;
    if (peek() instanceof Indent) {
      node = new BlockCommentNode();
      node.setBlock(block());
    } else {
      node = new CommentNode();
    }
    node.setBuffered(token.isBuffer());
    node.setLineNumber(token.getLineNumber());
    node.setFileName(filename);
    node.setValue(token.getValue());

    return node;
  }
Beispiel #7
0
  private Node parseTag() {
    // ast-filter look-ahead
    int i = 2;
    if (lookahead(i) instanceof Attribute) {
      i++;
    }
    if (lookahead(i) instanceof Colon) {
      i++;
      if (lookahead(i) instanceof Indent) {
        return this.parseASTFilter();
      }
    }
    Token token = nextToken();
    String name = token.getValue();
    TagNode tagNode = new TagNode();
    tagNode.setLineNumber(lexer.getLineno());
    tagNode.setFileName(filename);
    tagNode.setName(name);
    tagNode.setValue(name);

    while (true) {
      Token incomingToken = peek();
      if (incomingToken instanceof CssId) {
        Token tok = nextToken();
        tagNode.addAttribute("id", tok.getValue());
        continue;
      } else if (incomingToken instanceof CssClass) {
        Token tok = nextToken();
        tagNode.addAttribute("class", tok.getValue());
        continue;
      } else if (incomingToken instanceof Attribute) {
        Attribute tok = (Attribute) nextToken();
        tagNode.addAttributes(tok.getAttributes());
        continue;
      } else {
        break;
      }
    }

    // check immediate '.'
    boolean dot = false;
    if (peek() instanceof Dot) {
      dot = true;
      tagNode.setTextOnly(true);
      nextToken();
    }

    // (text | code | ':')?
    if (peek() instanceof Text) {
      tagNode.setTextNode(parseText());
    } else if (peek() instanceof Expression) {
      tagNode.setCodeNode(parseCode());
    } else if (peek() instanceof Colon) {
      Token next = nextToken();
      BlockNode block = new BlockNode();
      block.setLineNumber(next.getLineNumber());
      block.setFileName(filename);
      tagNode.setBlock(block);
      block.push(parseExpr());
    }

    // newline*
    while (peek() instanceof Newline) {
      nextToken();
    }

    if (!tagNode.isTextOnly()) {
      if (Arrays.asList(textOnlyTags).contains(tagNode.getName())) {
        tagNode.setTextOnly(true);
      }
    }

    // script special-case
    if ("script".equals(tagNode.getName())) {
      String type = tagNode.getAttribute("type");
      if (!dot && StringUtils.isNotBlank(type)) {
        String cleanType = type.replaceAll("^['\"]|['\"]$", "");
        if (!"text/javascript".equals(cleanType)) {
          tagNode.setTextOnly(false);
        }
      }
    }

    if (peek() instanceof Indent) {
      if (tagNode.isTextOnly()) {
        lexer.setPipeless(true);
        tagNode.setTextNode(parseTextBlock());
        lexer.setPipeless(false);
      } else {
        Node blockNode = block();
        if (tagNode.hasBlock()) {
          tagNode.getBlock().getNodes().addAll(blockNode.getNodes());
        } else {
          tagNode.setBlock(blockNode);
        }
      }
    }

    return tagNode;
  }