Search in sources :

Example 21 with SourceLocation

use of com.google.template.soy.base.SourceLocation in project closure-templates by google.

the class ParseErrors method reportTokenMgrError.

static void reportTokenMgrError(ErrorReporter reporter, String filePath, TokenMgrError exception) {
    int errorCode = exception.errorCode;
    String message = exception.getMessage();
    SourceLocation location;
    Matcher loc = EXTRACT_LOCATION.matcher(message);
    if (loc.find()) {
        int line = Integer.parseInt(loc.group(1));
        // javacc's column numbers are 0-based, while Soy's are 1-based
        int column = Integer.parseInt(loc.group(2)) + 1;
        location = new SourceLocation(filePath, line, column, line, column);
    } else {
        location = new SourceLocation(filePath);
    }
    // also allow us to avoid using a regex to extract line number information.
    if (exception.errorCode == TokenMgrError.LEXICAL_ERROR && message.contains("<EOF>")) {
        reporter.report(location, UNEXPECTED_EOF);
    } else {
        reporter.report(location, UNEXPECTED_TOKEN_MGR_ERROR, errorCode, message);
    }
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) CharMatcher(com.google.common.base.CharMatcher) Matcher(java.util.regex.Matcher)

Example 22 with SourceLocation

use of com.google.template.soy.base.SourceLocation in project closure-templates by google.

the class ParseExpressionTest method testParseDataReference.

@Test
public void testParseDataReference() throws Exception {
    SourceLocation loc = SourceLocation.UNKNOWN;
    ExprNode dataRef = assertThatExpression("$boo").isValidExpression();
    assertNodeEquals(new VarRefNode("boo", loc, false, null), dataRef);
    dataRef = assertThatExpression("$boo.foo").isValidExpression();
    assertNodeEquals(new FieldAccessNode(new VarRefNode("boo", loc, false, null), "foo", loc, false), dataRef);
    dataRef = assertThatExpression("$boo[0][$foo]").isValidExpression();
    assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, false, null), new IntegerNode(0, loc), loc, false), new VarRefNode("foo", loc, false, null), loc, false), dataRef);
    dataRef = assertThatExpression("$boo?[0]?[$foo]").isValidExpression();
    assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, false, null), new IntegerNode(0, loc), loc, true), new VarRefNode("foo", loc, false, null), loc, true), dataRef);
    dataRef = assertThatExpression("$ij.boo?[0][$ij.foo]").isValidExpression();
    assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, true, null), new IntegerNode(0, loc), loc, true), new VarRefNode("foo", loc, true, null), loc, false), dataRef);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ExprNode(com.google.template.soy.exprtree.ExprNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) ItemAccessNode(com.google.template.soy.exprtree.ItemAccessNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 23 with SourceLocation

use of com.google.template.soy.base.SourceLocation in project closure-templates by google.

the class TemplateRegistryTest method testBasicTemplatesWithSameNamesInDifferentFiles.

@Test
public void testBasicTemplatesWithSameNamesInDifferentFiles() {
    TemplateRegistry registry = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create("{namespace ns}\n" + "/** Template. */\n" + "{template .foo}\n" + "{/template}\n", SoyFileKind.SRC, "bar.soy"), SoyFileSupplier.Factory.create("{namespace ns2}\n" + "/** Template. */\n" + "{template .foo}\n" + "{/template}\n", SoyFileKind.SRC, "baz.soy")).parse().registry();
    assertThatRegistry(registry).containsBasicTemplate("ns.foo").definedAt(new SourceLocation("bar.soy", 3, 1, 3, 15));
    assertThatRegistry(registry).containsBasicTemplate("ns2.foo").definedAt(new SourceLocation("baz.soy", 3, 1, 3, 15));
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) Test(org.junit.Test)

Aggregations

SourceLocation (com.google.template.soy.base.SourceLocation)23 Test (org.junit.Test)8 VarRefNode (com.google.template.soy.exprtree.VarRefNode)5 TemplateNode (com.google.template.soy.soytree.TemplateNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)3 Matcher (java.util.regex.Matcher)3 CharMatcher (com.google.common.base.CharMatcher)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 FunctionNode (com.google.template.soy.exprtree.FunctionNode)2 NullNode (com.google.template.soy.exprtree.NullNode)2 HtmlOpenTagNode (com.google.template.soy.soytree.HtmlOpenTagNode)2 PrintNode (com.google.template.soy.soytree.PrintNode)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Point (com.google.template.soy.base.SourceLocation.Point)1 Identifier (com.google.template.soy.base.internal.Identifier)1 SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)1