Ich möchte JSP-Dateien zu identifizieren, um die Richtlinie Tags, Jsp Aktionselemente, Scriptlet Ausdruck etc. zu identifizieren. Dafür versuche ich, die vorhandene HTML-Code grammar zu erweitern. Ich habe Probleme mit der Unterscheidung von normalen HTML-Tags und JSP-Tags. Die bestehende Grammatik hat die folgenden Lexer Regeln:Antlr4 Parsing Jsp Scriptlets Ausdruck Direktive
TAG_OPEN
: '<' -> pushMode(TAG)
;
//
// tag declarations
//
mode TAG;
TAG_CLOSE
: '>' -> popMode
;
TAG_SLASH_CLOSE
: '/>' -> popMode
;
TAG_SLASH
: '/'
;
//
// lexing mode for attribute values
//
TAG_EQUALS
: '=' -> pushMode(ATTVALUE)
;
TAG_NAME
: TAG_NameStartChar TAG_NameChar*
| TAG_NameStartChar* ':' TAG_NameStartChar*
;
TAG_WHITESPACE
: [ \t\r\n] -> skip
;
Und die correspoding Parser-Regel für die:
htmlElement
: TAG_OPEN htmlTagName htmlAttribute* TAG_CLOSE htmlContent TAG_OPEN TAG_SLASH htmlTagName TAG_CLOSE
| TAG_OPEN htmlTagName htmlAttribute* TAG_SLASH_CLOSE
| TAG_OPEN htmlTagName htmlAttribute* TAG_CLOSE
| TAG_OPEN TAG_SLASH htmlTagName TAG_CLOSE
// | jspDirective
// | jspDeclaration
| scriptlet
| script
| style
;
Ich versuche, die oben genannten Regeln mit dem folgenden zu erweitern:
scriptlet
: JSP_TAG_OPEN jspElement jspElementName jspElementAttribute* JSP_TAG_CLOSE
//|SCRIPTLET
;
jspElementName
: TAG_NAME
;
jspElementAttribute
: jspAttributeName TAG_EQUALS jspAttributeValue
;
jspAttributeName
: TAG_NAME
;
jspAttributeValue
: ATTVALUE_VALUE
;
jspElement
: jspDirective
| jspExpression
| jspDeclaration
| jspFragment
;
jspDirective
: JSP_DIRECTIVE_SIGN
;
jspExpression
: JSP_EXPRESSION_SIGN
;
jspDeclaration
: JSP_DECLARATION_SIGN
;
jspFragment
: JSP_FRAGMENT_SIGN
;
Die Lexerregeln sind:
JSP_TAG_OPEN
: '<%' ' '*
;
JSP_DIRECTIVE_SIGN
: '@'
;
JSP_EXPRESSION_SIGN
: '='
;
JSP_DECLARATION_SIGN
: '!'
;
JSP_FRAGMENT_SIGN
: [ ]+
;
JSP_TAG_CLOSE
: '%>'
;
Aber das Problem, das ich denke, ist, dass, wann immer ein <
Zeichen die lexer Regel für TAG_OPEN
beginnt, zu laufen, anstatt zu warten, um zu sehen, ob das ein jsp Tag <%
ist.
Also, wenn ich versuche, eine kleine html mit folgendem Inhalt zu analysieren:
<html>
<title>Statement Load page</title>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
ich diese Störung erhalte:
line 3:2 no viable alternative at input '@ taglib uri="http://jakarta.apache.org/struts/tags-html"\r\n\tprefix="html"%>\r\n'
Ich habe meine Grammatik wurde Modifizieren den Script-Tag in mehr Details zu analysieren. Aber ich habe Probleme beim Parsen. @Bart Kiers könntest du bitte einen Blick darauf werfen. –