Search in sources :

Example 1 with SoyDocParam

use of com.google.template.soy.soytree.defn.SoyDocParam in project closure-templates by google.

the class TemplateNodeBuilder method parseSoyDocDeclsHelper.

/**
 * Private helper for the constructor to parse the SoyDoc declarations.
 *
 * @param cleanedSoyDoc The cleaned SoyDoc text. Must not be null.
 * @return A SoyDocDeclsInfo object with the parsed info.
 */
private List<SoyDocParam> parseSoyDocDeclsHelper(String originalSoyDoc, String cleanedSoyDoc, SourceLocation soyDocSourceLocation) {
    List<SoyDocParam> params = new ArrayList<>();
    RawTextNode originalSoyDocAsNode = new RawTextNode(-1, originalSoyDoc, soyDocSourceLocation);
    Matcher matcher = SOY_DOC_DECL_PATTERN.matcher(cleanedSoyDoc);
    // Important: This statement finds the param for the first iteration of the loop.
    boolean isFound = matcher.find();
    while (isFound) {
        // Save the match groups.
        String declKeyword = matcher.group(1);
        String declText = matcher.group(2);
        String fullMatch = matcher.group();
        // find the param in the original soy doc and use the RawTextNode support for
        // calculating substring locations to get a more accurate location
        int indexOfParamName = originalSoyDoc.indexOf(declText, originalSoyDoc.indexOf(fullMatch));
        SourceLocation paramLocation = originalSoyDocAsNode.substringLocation(indexOfParamName, indexOfParamName + declText.length());
        // Find the next declaration in the SoyDoc and extract this declaration's desc string.
        int descStart = matcher.end();
        // Important: This statement finds the param for the next iteration of the loop.
        // We must find the next param now in order to know where the current param's desc ends.
        isFound = matcher.find();
        int descEnd = (isFound) ? matcher.start() : cleanedSoyDoc.length();
        String desc = cleanedSoyDoc.substring(descStart, descEnd).trim();
        if (declKeyword.equals("@param") || declKeyword.equals("@param?")) {
            if (SOY_DOC_PARAM_TEXT_PATTERN.matcher(declText).matches()) {
                params.add(new SoyDocParam(declText, declKeyword.equals("@param"), desc, paramLocation));
            } else {
                if (declText.startsWith("{")) {
                    // v1 is allowed for compatibility reasons
                    if (!isMarkedV1) {
                        errorReporter.report(paramLocation, LEGACY_COMPATIBLE_PARAM_TAG, declText);
                    }
                } else {
                    errorReporter.report(paramLocation, INVALID_SOYDOC_PARAM, declText);
                }
            }
        } else {
            throw new AssertionError();
        }
    }
    return params;
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) Matcher(java.util.regex.Matcher) CharMatcher(com.google.common.base.CharMatcher) ArrayList(java.util.ArrayList) SoyDocParam(com.google.template.soy.soytree.defn.SoyDocParam)

Example 2 with SoyDocParam

use of com.google.template.soy.soytree.defn.SoyDocParam in project closure-templates by google.

the class TemplateNodeTest method testParseSoyDoc.

@Test
public void testParseSoyDoc() {
    String soyDoc = "" + "/**\n" + " * Test template.\n" + " *\n" + " * @param foo Foo to print.\n" + " * @param? goo\n" + " *     Goo to print.\n" + " */";
    TemplateNode tn = parse("{namespace ns}\n" + "/**\n" + " * Test template.\n" + " *\n" + " * @param foo Foo to print.\n" + " * @param? goo\n" + " *     Goo to print.\n" + " */" + "{template .boo}{$foo}{$goo}{/template}");
    assertEquals(soyDoc, tn.getSoyDoc());
    assertEquals("Test template.", tn.getSoyDocDesc());
    List<TemplateParam> params = tn.getParams();
    assertEquals(2, params.size());
    SoyDocParam soyDocParam0 = (SoyDocParam) params.get(0);
    assertEquals(DeclLoc.SOY_DOC, soyDocParam0.declLoc());
    assertEquals("foo", soyDocParam0.name());
    assertEquals(true, soyDocParam0.isRequired());
    assertEquals("Foo to print.", soyDocParam0.desc());
    SoyDocParam soyDocParam1 = (SoyDocParam) params.get(1);
    assertEquals(DeclLoc.SOY_DOC, soyDocParam1.declLoc());
    assertEquals("goo", soyDocParam1.name());
    assertEquals(false, soyDocParam1.isRequired());
    assertEquals("Goo to print.", soyDocParam1.desc());
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) SoyDocParam(com.google.template.soy.soytree.defn.SoyDocParam) Test(org.junit.Test)

Example 3 with SoyDocParam

use of com.google.template.soy.soytree.defn.SoyDocParam in project closure-templates by google.

the class TemplateNodeTest method testParseHeaderDecls.

@Test
public void testParseHeaderDecls() {
    TemplateNode tn = parse("{namespace ns}\n" + "/**@param foo */\n" + "{template .boo}{$foo}{/template}");
    List<TemplateParam> params = tn.getParams();
    assertThat(params).hasSize(1);
    SoyDocParam soyDocParam0 = (SoyDocParam) params.get(0);
    assertEquals("foo", soyDocParam0.name());
    assertThat(ImmutableList.copyOf(tn.getAllParams())).hasSize(1);
}
Also used : TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) SoyDocParam(com.google.template.soy.soytree.defn.SoyDocParam) Test(org.junit.Test)

Aggregations

SoyDocParam (com.google.template.soy.soytree.defn.SoyDocParam)3 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)2 Test (org.junit.Test)2 CharMatcher (com.google.common.base.CharMatcher)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1