mirror of
https://github.com/github/codeql.git
synced 2026-05-02 20:25:13 +02:00
JS: Add parser for angular expressions
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.semmle.jcorn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.semmle.js.ast.CallExpression;
|
||||
import com.semmle.js.ast.Expression;
|
||||
import com.semmle.js.ast.Position;
|
||||
import com.semmle.js.ast.SourceLocation;
|
||||
|
||||
/**
|
||||
* Parser for Angular template expressions, based on the JS parser with
|
||||
* modified handling of the pipe operator.
|
||||
*/
|
||||
public class AngularExpressionParser extends Parser {
|
||||
public AngularExpressionParser(String input, int startPos) {
|
||||
super(new Options(), input, startPos);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Expression buildBinary(
|
||||
int startPos,
|
||||
Position startLoc,
|
||||
Expression left,
|
||||
Expression right,
|
||||
String op,
|
||||
boolean logical) {
|
||||
// Angular pipe expression: `x|f:a` is desugared to `f(x, a)`
|
||||
if (op.equals("|")) {
|
||||
DestructuringErrors refDestructuringErrors = new DestructuringErrors();
|
||||
List<Expression> arguments = new ArrayList<>();
|
||||
arguments.add(left);
|
||||
while (this.type == TokenType.colon) {
|
||||
this.next();
|
||||
int argStartPos = this.pos;
|
||||
Position argStartLocation = this.curPosition();
|
||||
Expression arg = parseMaybeUnary(refDestructuringErrors, false);
|
||||
arguments.add(parseExprOp(arg, argStartPos, argStartLocation, TokenType.plusMin.binop, true));
|
||||
}
|
||||
SourceLocation loc = new SourceLocation(startLoc);
|
||||
return this.finishNode(new CallExpression(loc, right, null, arguments, false, false));
|
||||
}
|
||||
return super.buildBinary(startPos, startLoc, left, right, op, logical);
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,21 @@ package com.semmle.jcorn;
|
||||
import static com.semmle.jcorn.Whitespace.isNewLine;
|
||||
import static com.semmle.jcorn.Whitespace.lineBreak;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.semmle.jcorn.Identifiers.Dialect;
|
||||
import com.semmle.jcorn.Options.AllowReserved;
|
||||
import com.semmle.jcorn.TokenType.Properties;
|
||||
import com.semmle.js.ast.ArrayExpression;
|
||||
import com.semmle.js.ast.ArrayPattern;
|
||||
import com.semmle.js.ast.ArrowFunctionExpression;
|
||||
@@ -45,7 +57,6 @@ import com.semmle.js.ast.INode;
|
||||
import com.semmle.js.ast.IPattern;
|
||||
import com.semmle.js.ast.Identifier;
|
||||
import com.semmle.js.ast.IfStatement;
|
||||
import com.semmle.js.ast.FieldDefinition;
|
||||
import com.semmle.js.ast.ImportDeclaration;
|
||||
import com.semmle.js.ast.ImportDefaultSpecifier;
|
||||
import com.semmle.js.ast.ImportNamespaceSpecifier;
|
||||
@@ -95,18 +106,6 @@ import com.semmle.util.data.StringUtil;
|
||||
import com.semmle.util.exception.CatastrophicError;
|
||||
import com.semmle.util.exception.Exceptions;
|
||||
import com.semmle.util.io.WholeIO;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Java port of Acorn.
|
||||
@@ -1462,7 +1461,7 @@ public class Parser {
|
||||
return left;
|
||||
}
|
||||
|
||||
private Expression buildBinary(
|
||||
protected Expression buildBinary(
|
||||
int startPos,
|
||||
Position startLoc,
|
||||
Expression left,
|
||||
|
||||
Reference in New Issue
Block a user