This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit 1c9dca4a76ddca724accd1f814aac9d91b64dd62
Author: Josh Tynjala <[email protected]>
AuthorDate: Tue Apr 23 10:31:27 2024 -0700

    CSSDocument: if lexer has errors, don't continue to parser, and if parser 
has errors, don't continue to tree walker
    
    If the CSS is invalid at the lexer or parser level, the tree walker will 
get confused by missing/wrong tokens
---
 RELEASE_NOTES.md                                   |  2 +-
 .../royale/compiler/internal/css/CSSDocument.java  | 58 +++++++++++++++-------
 2 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 59198c20a..b3b891157 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -25,7 +25,7 @@ Apache Royale Compiler 0.9.11
 - compiler: Fix incorrect error or warning positions for CSS content inside 
`<fx:Style>` tag.
 - compiler: Fix non-string values in an MXML array sometimes getting 
incorrectly wrapped in quotes when emitting JavaScript.
 - compiler: Fix null exception for `<fx:Style>` tags that contain only 
comments.
-- compiler: Fix null exception for `<fx:Style>` tags that contain invalid CSS.
+- compiler: Fix exceptions for `<fx:Style>` tags that contain invalid CSS.
 - compiler: Fix crash when attempting to use `--remove-circulars=false` with a 
release build.
 - debugger: Fix exception when evaluating certain expressions at run-time.
 - formatter: Added `insert-new-line-else` configuration option.
diff --git 
a/compiler/src/main/java/org/apache/royale/compiler/internal/css/CSSDocument.java
 
b/compiler/src/main/java/org/apache/royale/compiler/internal/css/CSSDocument.java
index b59c4d521..91e467027 100644
--- 
a/compiler/src/main/java/org/apache/royale/compiler/internal/css/CSSDocument.java
+++ 
b/compiler/src/main/java/org/apache/royale/compiler/internal/css/CSSDocument.java
@@ -31,11 +31,14 @@ import org.antlr.runtime.RecognitionException;
 import org.antlr.runtime.TokenStream;
 import org.antlr.runtime.tree.CommonTree;
 import org.antlr.runtime.tree.CommonTreeNodeStream;
+import org.apache.royale.compiler.clients.problems.CompilerProblemCategorizer;
 import org.apache.royale.compiler.css.ICSSDocument;
 import org.apache.royale.compiler.css.ICSSFontFace;
 import org.apache.royale.compiler.css.ICSSNamespaceDefinition;
 import org.apache.royale.compiler.css.ICSSNode;
 import org.apache.royale.compiler.css.ICSSRule;
+import org.apache.royale.compiler.problems.CSSParserProblem;
+import org.apache.royale.compiler.problems.CompilerProblemSeverity;
 import org.apache.royale.compiler.problems.ICompilerProblem;
 import org.apache.royale.compiler.problems.UnexpectedExceptionProblem;
 
@@ -70,28 +73,35 @@ public class CSSDocument extends CSSNodeBase implements 
ICSSDocument
             // parse and build tree
             final CSSLexer lexer = new CSSLexer(input);
             final CommonTokenStream tokens = new CommonTokenStream(lexer);
-            final CSSParser parser = new CSSParser(tokens);
-            final CSSParser.stylesheet_return stylesheet = parser.stylesheet();
-            CommonTree ast = (CommonTree)stylesheet.getTree();
-            if (ast == null)
+            problems.addAll(lexer.problems);
+            if (!hasErrors(lexer.problems))
             {
-                // may be null if the input contains only comments -JT
-                // apache/royale-compiler#1218
-                ast = new CommonTree();
-            }
-            final CommonTreeNodeStream nodes = new CommonTreeNodeStream(ast);
-            nodes.setTokenStream(tokens);
+                final CSSParser parser = new CSSParser(tokens);
+                final CSSParser.stylesheet_return stylesheet = 
parser.stylesheet();
+                problems.addAll(parser.problems);
+                if (!hasErrors(parser.problems))
+                {
+                    CommonTree ast = (CommonTree)stylesheet.getTree();
+                    if (ast == null)
+                    {
+                        // may be null if the input contains only comments -JT
+                        // apache/royale-compiler#1218
+                        ast = new CommonTree();
+                    }
+                    final CommonTreeNodeStream nodes = new 
CommonTreeNodeStream(ast);
+                    nodes.setTokenStream(tokens);
 
-            // walk the tree and build definitions
-            final CSSTree treeWalker = new CSSTree(nodes);
-            treeWalker.stylesheet();
+                    // walk the tree and build definitions
+                    final CSSTree treeWalker = new CSSTree(nodes);
+                    treeWalker.stylesheet();
 
-            problems.addAll(lexer.problems);
-            problems.addAll(parser.problems);
-            problems.addAll(treeWalker.problems);
+                    problems.addAll(treeWalker.problems);
 
-            // definition models
-            return treeWalker.model;
+                    // definition models
+                    return treeWalker.model;
+                }
+            }
+            return null;
         }
         catch (RecognitionException e)
         {
@@ -233,4 +243,16 @@ public class CSSDocument extends CSSNodeBase implements 
ICSSDocument
     {
         return namespacesLookup.get(DEFAULT_NAMESPACE_SHORT_NAME);
     }
+
+       private static boolean hasErrors(Collection<CSSParserProblem> problems) 
{
+               CompilerProblemCategorizer categorizer = new 
CompilerProblemCategorizer(null);
+               for (ICompilerProblem problem : problems) {
+                       CompilerProblemSeverity severity = 
categorizer.getProblemSeverity(problem);
+                       if (CompilerProblemSeverity.ERROR.equals(severity)) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
 }

Reply via email to