Port js (es5) module

This commit is contained in:
Clément Fournier
2020-03-17 18:00:23 +01:00
parent c07535927f
commit e23df57ccd
6 changed files with 24 additions and 144 deletions

View File

@ -12,17 +12,17 @@ options {
// JDK_VERSION = "1.5";
}
PARSER_BEGIN(Ecmascript5Parser)
PARSER_BEGIN(Ecmascript5ParserImpl)
package net.sourceforge.pmd.lang.ecmascript5.ast;
import net.sourceforge.pmd.lang.ast.CharStream;
import net.sourceforge.pmd.lang.ast.TokenMgrError;
public class Ecmascript5Parser {
public class Ecmascript5ParserImpl {
}
PARSER_END(Ecmascript5Parser)
PARSER_END(Ecmascript5ParserImpl)
/* WHITE SPACE - 7.2 */
<DEFAULT,NOREGEXP,NOEOL>
@ -756,9 +756,9 @@ TOKEN :
| < NE: "!=" > : DEFAULT
| < SC_OR: "||" > : DEFAULT
| < SC_AND: "&&" > : DEFAULT
| < INCR: "++" > { if (matchedToken.specialToken != null && matchedToken.specialToken.kind == EOL) { matchedToken.kind = NL_INCR; } } : DEFAULT
| < INCR: "++" > { if (matchedToken.specialToken != null && matchedToken.specialToken.kind == EOL) { matchedToken = matchedToken.withKind(NL_INCR); } } : DEFAULT
| < NL_INCR: [ "\r" , "\n" , "\u2028" , "\u2029"] "++" > : DEFAULT
| < DECR: "--" > { if (matchedToken.specialToken != null && matchedToken.specialToken.kind == EOL) { matchedToken.kind = NL_DECR; } } : DEFAULT
| < DECR: "--" > { if (matchedToken.specialToken != null && matchedToken.specialToken.kind == EOL) { matchedToken = matchedToken.withKind(NL_DECR); } } : DEFAULT
| < NL_DECR: [ "\r" , "\n" , "\u2028" , "\u2029"] "--" > : DEFAULT
| < PLUS: "+" > : DEFAULT
| < MINUS: "-" > : DEFAULT

View File

@ -37,8 +37,10 @@
<phase>generate-sources</phase>
<configuration>
<target>
<ant antfile="src/main/ant/alljavacc.xml">
<property name="target" value="${project.build.directory}/generated-sources/javacc" />
<ant antfile="${javacc.ant.wrapper}" target="alljavacc">
<property name="no-jjtree" value="true"/> <!-- This is a CPD module -->
<property name="lang-name" value="Ecmascript5" />
<property name="lang-terse-name" value="ecmascript5" />
<property name="javacc.jar" value="${javacc.jar}" />
</ant>
</target>

View File

@ -1,111 +0,0 @@
<project name="pmd" default="alljavacc" basedir="../../">
<property name="javacc-home.path" value="target/lib" />
<target name="alljavacc"
description="Generates all JavaCC aspects within PMD"
depends="checkUpToDate,init,es5javacc,cleanup" />
<target name="checkUpToDate">
<uptodate property="javaccBuildNotRequired" targetfile="${target}/last-generated-timestamp">
<srcfiles dir="etc/grammar" includes="*.jj*"/>
</uptodate>
<echo message="up to date check: javaccBuildNotRequired=${javaccBuildNotRequired}"/>
</target>
<target name="init" unless="javaccBuildNotRequired">
<mkdir dir="${javacc-home.path}" />
<copy file="${javacc.jar}" tofile="${javacc-home.path}/javacc.jar" />
<mkdir dir="${target}"/>
<touch file="${target}/last-generated-timestamp"/>
</target>
<target name="cleanup">
<delete dir="${javacc-home.path}" />
</target>
<target name="es5javacc" description="Generates the Ecmascript 5 grammar" unless="javaccBuildNotRequired">
<delete dir="${target}/net/sourceforge/pmd/lang/ecmascript5/ast" />
<mkdir dir="${target}/net/sourceforge/pmd/lang/ecmascript5/ast" />
<!-- Ensure generated using CharStream interface -->
<javacc static="false"
usercharstream="true"
target="etc/grammar/es5.jj"
outputdirectory="${target}/net/sourceforge/pmd/lang/ecmascript5/ast"
javacchome="${javacc-home.path}" />
<replace file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/Ecmascript5ParserTokenManager.java"
token="class Ecmascript5ParserTokenManager"
value="class Ecmascript5ParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />
<delete file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/CharStream.java" />
<delete file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/ParseException.java" />
<delete file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/TokenMgrError.java" />
<replace file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/Token.java">
<replacetoken>public class Token implements java.io.Serializable</replacetoken>
<replacevalue><![CDATA[import net.sourceforge.pmd.lang.ast.GenericToken;
public class Token implements GenericToken, java.io.Serializable]]></replacevalue>
</replace>
<!--Add implementation methods of GenericToken-->
<replace file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/Token.java">
<replacetoken>public Token specialToken;</replacetoken>
<replacevalue><![CDATA[public Token specialToken;
@Override
public GenericToken getNext() {
return next;
}
@Override
public GenericToken getPreviousComment() {
return specialToken;
}
@Override
public String getImage() {
return image;
}
@Override
public int getBeginLine() {
return beginLine;
}
@Override
public int getEndLine() {
return endLine;
}
@Override
public int getBeginColumn() {
return beginColumn;
}
@Override
public int getEndColumn() {
return endColumn;
}
]]></replacevalue>
</replace>
<replaceregexp>
<regexp pattern="class|interface" />
<substitution expression="@Deprecated @net.sourceforge.pmd.annotation.InternalApi \0" />
<fileset dir="${target}/net/sourceforge/pmd/lang/ecmascript5/ast">
<exclude name="AST*.java" />
</fileset>
</replaceregexp>
<replaceregexp>
<regexp pattern="public class ParseException " />
<substitution expression=" /** @deprecated Use superclass {@link net.sourceforge.pmd.lang.ast.ParseException} */
@Deprecated @net.sourceforge.pmd.annotation.InternalApi \0" />
<fileset file="${target}/net/sourceforge/pmd/lang/ecmascript5/ast/ParseException.java"/>
</replaceregexp>
</target>
</project>

View File

@ -8,16 +8,15 @@ import java.io.StringReader;
import net.sourceforge.pmd.cpd.internal.JavaCCTokenizer;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.GenericToken;
import net.sourceforge.pmd.lang.ecmascript5.Ecmascript5TokenManager;
import net.sourceforge.pmd.lang.ecmascript5.ast.Ecmascript5ParserConstants;
import net.sourceforge.pmd.lang.ecmascript5.ast.Token;
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
import net.sourceforge.pmd.lang.ecmascript5.ast.Ecmascript5TokenKinds;
import net.sourceforge.pmd.lang.ecmascript5.ast.Ecmascript5TokenManager;
import net.sourceforge.pmd.util.IOUtil;
/**
* The Ecmascript Tokenizer
*/
public class EcmascriptTokenizer extends JavaCCTokenizer {
public class EcmascriptTokenizer extends JavaCCTokenizer<JavaccToken> {
@Override
protected TokenManager getLexerForSource(SourceCode sourceCode) {
@ -26,18 +25,12 @@ public class EcmascriptTokenizer extends JavaCCTokenizer {
}
@Override
protected TokenEntry processToken(Tokens tokenEntries, GenericToken currentToken, String filename) {
return new TokenEntry(getTokenImage(currentToken), filename, currentToken.getBeginLine(),
currentToken.getBeginColumn(), currentToken.getEndColumn());
}
private String getTokenImage(GenericToken token) {
Token jsToken = (Token) token;
protected String getImage(JavaccToken jsToken) {
// Remove line continuation characters from string literals
if (jsToken.kind == Ecmascript5ParserConstants.STRING_LITERAL
|| jsToken.kind == Ecmascript5ParserConstants.UNTERMINATED_STRING_LITERAL) {
return token.getImage().replaceAll("(?<!\\\\)\\\\(\\r\\n|\\r|\\n)", "");
if (jsToken.kind == Ecmascript5TokenKinds.STRING_LITERAL
|| jsToken.kind == Ecmascript5TokenKinds.UNTERMINATED_STRING_LITERAL) {
return jsToken.getImage().replaceAll("(?<!\\\\)\\\\(\\r\\n|\\r|\\n)", "");
}
return token.getImage();
return jsToken.getImage();
}
}

View File

@ -11,7 +11,7 @@ import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTAstRoot;
import net.sourceforge.pmd.lang.ecmascript5.Ecmascript5TokenManager;
import net.sourceforge.pmd.lang.ecmascript5.ast.Ecmascript5TokenManager;
/**
* Adapter for the EcmascriptParser.

View File

@ -1,25 +1,21 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript5;
package net.sourceforge.pmd.lang.ecmascript5.ast;
import java.io.Reader;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory;
import net.sourceforge.pmd.lang.ecmascript5.ast.Ecmascript5ParserTokenManager;
/**
* Ecmascript 5 Token Manager implementation.
*
* @deprecated This is internal API
*/
@Deprecated
@InternalApi
public class Ecmascript5TokenManager implements TokenManager {
private final Ecmascript5ParserTokenManager tokenManager;
private final Ecmascript5ParserImplTokenManager tokenManager;
/**
* Creates a new Ecmascript 5 Token Manager from the given source code.
@ -28,7 +24,7 @@ public class Ecmascript5TokenManager implements TokenManager {
* the source code
*/
public Ecmascript5TokenManager(Reader source) {
tokenManager = new Ecmascript5ParserTokenManager(CharStreamFactory.simpleCharStream(source));
tokenManager = new Ecmascript5ParserImplTokenManager(CharStreamFactory.simpleCharStream(source));
}
@Override
@ -38,6 +34,6 @@ public class Ecmascript5TokenManager implements TokenManager {
@Override
public void setFileName(String fileName) {
Ecmascript5ParserTokenManager.setFileName(fileName);
Ecmascript5ParserImplTokenManager.setFileName(fileName);
}
}