Skip to content

Commit

Permalink
Several parsing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Apr 19, 2024
1 parent 006ddbb commit 2086900
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 17 deletions.
9 changes: 8 additions & 1 deletion src/main/antlr/BoxTemplateGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ scriptBody: SCRIPT_BODY*;
// <bx:script> statements... </cfscript>
script: SCRIPT_OPEN scriptBody SCRIPT_END_BODY;

/*
<bx:return>
<bx:return />
<bx:return expression>
<bx:return expression />
<bx:return 10/5 >
<bx:return 20 / 7 />
*/
return:
// <bx:return> or... <bx:return expression> or... <bx:return expression />
COMPONENT_OPEN PREFIX RETURN expression? (
COMPONENT_SLASH_CLOSE
| COMPONENT_CLOSE
Expand Down
25 changes: 22 additions & 3 deletions src/main/antlr/BoxTemplateLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ options {
}
}
*/
COMMENT: '<!---' .*? '--->' -> channel(HIDDEN);

COMMENT_START: '<!---' -> pushMode(COMMENT), channel(HIDDEN);

WS: (' ' | '\t' | '\r'? '\n')+;

Expand All @@ -49,6 +50,16 @@ ICHAR_1: '#' -> type(CONTENT_TEXT);

CONTENT_TEXT: ~[<#]+;

// *********************************************************************************************************************
mode COMMENT;

COMMENT_END: '--->' -> popMode, channel(HIDDEN);

COMMENT_START2:
'<!---' -> pushMode(COMMENT), type(COMMENT_START), channel(HIDDEN);

COMMENT_TEXT: .+? -> channel(HIDDEN);

// *********************************************************************************************************************
mode COMPONENT_MODE;

Expand All @@ -60,7 +71,10 @@ FUNCTION: 'function';
ARGUMENT: 'argument';

SCRIPT: 'script' -> pushMode(XFSCRIPT);
RETURN: 'return' -> pushMode(EXPRESSION_MODE_COMPONENT);

// return may or may not have an expression, so eat any leading whitespace now so it doesn't give us an expression part that's just a space
RETURN:
'return' [ \t\r\n]* -> pushMode(EXPRESSION_MODE_COMPONENT);

IF: 'if' -> pushMode(EXPRESSION_MODE_COMPONENT);
ELSE: 'else';
Expand Down Expand Up @@ -170,10 +184,15 @@ OPEN_SINGLE:
// *********************************************************************************************************************
mode EXPRESSION_MODE_COMPONENT;

COMPONENT_SLASH_CLOSE1:
'/>' -> type(COMPONENT_SLASH_CLOSE), popMode, popMode, popMode;

COMPONENT_CLOSE1:
'>' -> type(COMPONENT_CLOSE), popMode, popMode, popMode;

EXPRESSION_PART: ~[>'"]+;
EXPRESSION_PART: ~[>'"/]+;
EXPRESSION_PART1: '/' -> type(EXPRESSION_PART);
OPEN_QUOTE2:
'"' -> pushMode(quotesModeExpression), type(OPEN_QUOTE);
Expand Down
10 changes: 2 additions & 8 deletions src/main/antlr/CFScriptGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,8 @@ property:
// /** Comment */
javadoc: JAVADOC_COMMENT;

// function() {} or () => {} or () -> {}
anonymousFunction: lambda | closure;

lambda:
// ( param, param ) -> {}
LPAREN functionParamList? RPAREN (postannotation)* ARROW anonymousFunctionBody
// param -> {}
| identifier ARROW anonymousFunctionBody;
// function() {} or () => {}
anonymousFunction: closure;

closure:
// function( param, param ) {}
Expand Down
11 changes: 9 additions & 2 deletions src/main/antlr/CFTemplateGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function:
// <cffunction name="foo" >
COMPONENT_OPEN PREFIX FUNCTION attribute* COMPONENT_CLOSE
// zero or more <cfargument ... >
whitespace? (argument whitespace?)*
(nonInterpolatedText? argument)* whitespace?
// code inside function
body = statements
// </cffunction>
Expand All @@ -175,8 +175,15 @@ scriptBody: SCRIPT_BODY*;
// <cfscript> statements... </cfscript>
script: SCRIPT_OPEN scriptBody SCRIPT_END_BODY;

/*
<cfreturn>
<cfreturn />
<cfreturn expression>
<cfreturn expression />
<cfreturn 10/5 >
<cfreturn 20 / 7 />
*/
return:
// <cfreturn> or... <cfreturn expression> or... <cfreturn expression />
COMPONENT_OPEN PREFIX RETURN expression? (
COMPONENT_SLASH_CLOSE
| COMPONENT_CLOSE
Expand Down
25 changes: 22 additions & 3 deletions src/main/antlr/CFTemplateLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ options {
}
}
*/
COMMENT: '<!---' .*? '--->' -> channel(HIDDEN);

COMMENT_START: '<!---' -> pushMode(COMMENT), channel(HIDDEN);

WS: (' ' | '\t' | '\r'? '\n')+;

Expand All @@ -49,6 +50,16 @@ ICHAR_1: '#' -> type(CONTENT_TEXT);

CONTENT_TEXT: ~[<#]+;

// *********************************************************************************************************************
mode COMMENT;

COMMENT_END: '--->' -> popMode, channel(HIDDEN);

COMMENT_START2:
'<!---' -> pushMode(COMMENT), type(COMMENT_START), channel(HIDDEN);

COMMENT_TEXT: .+? -> channel(HIDDEN);

// *********************************************************************************************************************
mode COMPONENT_MODE;

Expand All @@ -60,7 +71,10 @@ FUNCTION: 'function';
ARGUMENT: 'argument';

SCRIPT: 'script' -> pushMode(XFSCRIPT);
RETURN: 'return' -> pushMode(EXPRESSION_MODE_COMPONENT);

// return may or may not have an expression, so eat any leading whitespace now so it doesn't give us an expression part that's just a space
RETURN:
'return' [ \t\r\n]* -> pushMode(EXPRESSION_MODE_COMPONENT);

IF: 'if' -> pushMode(EXPRESSION_MODE_COMPONENT);
ELSE: 'else';
Expand Down Expand Up @@ -170,10 +184,15 @@ OPEN_SINGLE:
// *********************************************************************************************************************
mode EXPRESSION_MODE_COMPONENT;

COMPONENT_SLASH_CLOSE1:
'/>' -> type(COMPONENT_SLASH_CLOSE), popMode, popMode, popMode;

COMPONENT_CLOSE1:
'>' -> type(COMPONENT_CLOSE), popMode, popMode, popMode;

EXPRESSION_PART: ~[>'"]+;
EXPRESSION_PART: ~[>'"/]+;
EXPRESSION_PART1: '/' -> type(EXPRESSION_PART);
OPEN_QUOTE2:
'"' -> pushMode(quotesModeExpression), type(OPEN_QUOTE);
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/TestCases/components/BoxTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,37 @@ public void testLoopConditionExprScript() {
assertThat( variables.get( result ) ).isEqualTo( "12345" );
}

@Test
public void testNestedComments() {
instance.executeSource(
"""
<bx:set fruit = "">
<bx:switch expression="#fruit#">
<bx:case value="Apple">I like apples!</bx:case>
<bx:case value="Orange,Citrus">I like oranges!</bx:case>
<!---
<bx:case value="Kiwi">
<!--- nested comment --->
I like kiwi!
</bx:case>
--->
<bx:defaultcase>Fruit, what fruit?</bx:defaultcase>
</bx:switch>
""", context, BoxSourceType.BOXTEMPLATE );
}

@Test
public void testReturns() {
// Only the first one actually returns, but I just want to ensure they compile
instance.executeSource(
"""
<bx:return>
<bx:return />
<bx:return expression>
<bx:return expression />
<bx:return 10/5 >
<bx:return 20 / 7 />
""", context, BoxSourceType.BOXTEMPLATE );
}

}
46 changes: 46 additions & 0 deletions src/test/java/TestCases/components/CFTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -857,4 +857,50 @@ public void testLoopConditionExprScript() {
assertThat( variables.get( result ) ).isEqualTo( "12345" );
}

@Test
public void testExtraTextInFunctionArguments() {
instance.executeSource(
"""
<cffunction name="example" output="false">>
<cfargument name="test">foobar
<cfargument name="test2">
sdfsd
<cfreturn "yo">
</cffunction>
""", context, BoxSourceType.CFTEMPLATE );
}

@Test
public void testNestedComments() {
instance.executeSource(
"""
<cfset fruit = "">
<cfswitch expression="#fruit#">
<cfcase value="Apple">I like apples!</cfcase>
<cfcase value="Orange,Citrus">I like oranges!</cfcase>
<!---
<cfcase value="Kiwi">
<!--- nested comment --->
I like kiwi!
</cfcase>
--->
<cfdefaultcase>Fruit, what fruit?</cfdefaultcase>
</cfswitch>
""", context, BoxSourceType.CFTEMPLATE );
}

@Test
public void testReturns() {
// Only the first one actually returns, but I just want to ensure they compile
instance.executeSource(
"""
<cfreturn>
<cfreturn />
<cfreturn expression>
<cfreturn expression />
<cfreturn 10/5 >
<cfreturn 20 / 7 />
""", context, BoxSourceType.CFTEMPLATE );
}

}

0 comments on commit 2086900

Please sign in to comment.