Skip to content

Commit

Permalink
8285081: Improve XPath operators count accuracy
Browse files Browse the repository at this point in the history
Backport-of: 8e0783917975075aae5d586f0076d5093afb0b62
  • Loading branch information
GoeLin committed Jul 18, 2022
1 parent 6496396 commit 4b45646
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
* @see com.sun.java_cup.internal.runtime.virtual_parse_stack
* @author Frank Flannery
*
* @LastModified: Jan 2022
* @LastModified: June 2022
*/

public abstract class lr_parser {
Expand All @@ -149,6 +149,7 @@ public abstract class lr_parser {
private int grpCount = 0;
private int opCount = 0;
private int totalOpCount = 0;
private int lastSym;

/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
Expand Down Expand Up @@ -377,13 +378,17 @@ public Symbol scan() throws Exception {
opCount++; // function
isLiteral = false;
} else if (contains(sym.OPERATORS, s.sym)) {
opCount++;
// axis nodetest is counted as one step, so not counted if last=DCOLON
if (lastSym != sym.DCOLON) {
opCount++;
}
isLiteral = false;
}

if (s.sym == sym.Literal || s.sym == sym.QNAME) {
isLiteral = true;
}
lastSym = s.sym;

return s;
}
Expand Down Expand Up @@ -588,6 +593,7 @@ public Symbol parse() throws java.lang.Exception
isLiteral = false;
grpCount = 0;
opCount = 0;
lastSym = -1;

/* get the first token */
cur_token = scan();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@

package com.sun.org.apache.xalan.internal.xsltc.compiler;

import java.util.Arrays;

/**
* CUP generated class containing symbol constants.
* This class was generated by CUP v0.10j on Fri Feb 27 13:01:50 PST 2004.
*
* @LastModified: Jan 2022
* @LastModified: June 2022
*/
public class sym {
/* terminals */
Expand Down Expand Up @@ -92,9 +90,11 @@ public class sym {
/*
AXES: count once at DCOLON,
these axes names are therefore not counted:
NAMESPACE, FOLLOWINGSIBLING, CHILD, DESCENDANTORSELF, DESCENDANT
, PRECEDINGSIBLING, SELF, ANCESTORORSELF, PRECEDING, ANCESTOROR, PARENT, FOLLOWING, ATTRIBUTE
NAMESPACE, FOLLOWINGSIBLING, CHILD, DESCENDANTORSELF, DESCENDANT,
PRECEDINGSIBLING, SELF, ANCESTORORSELF, PRECEDING, ANCESTOROR, PARENT,
FOLLOWING, ATTRIBUTE
*/
public static final int[] OPERATORS = {GE, SLASH, ATSIGN, LPAREN, DCOLON,
MINUS, STAR, LT, OR, DIV, PLUS, LE, VBAR, MOD, EQ, LBRACK, DOLLAR, NE, GT};
public static final int[] OPERATORS = {GT, GE, EQ, NE, LT, LE, SLASH, DSLASH,
DOT, DDOT, ATSIGN, DCOLON, PLUS, MINUS, STAR, DIV, MOD, AND, OR, LPAREN,
LBRACK, VBAR, DOLLAR, NODE, TEXT, PI, PIPARAM};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* This class is in charge of lexical processing of the XPath
* expression into tokens.
*
* @LastModified: Apr 2022
* @LastModified: June 2022
*/
class Lexer
{
Expand Down Expand Up @@ -155,6 +155,7 @@ void tokenize(String pat, List<String> targetStrings)
boolean isStartOfPat = true;
boolean isAttrName = false;
boolean isNum = false;
boolean isAxis = false;

// Nesting of '[' so we can know if the given element should be
// counted inside the m_patternMap.
Expand Down Expand Up @@ -254,8 +255,7 @@ void tokenize(String pat, List<String> targetStrings)
// check operator symbol
String s = pat.substring(startSubstring, i);
if (Token.contains(s)) {
m_opCount++;
isLiteral = false;
incrementCount();
}
addToTokenQueue(s);
}
Expand Down Expand Up @@ -339,23 +339,45 @@ else if (Token.STAR == c)
{
nesting--;
}
else if ((Token.LPAREN == c) || (Token.LBRACK == c))
else if (Token.LBRACK == c)
{
nesting++;
if (!isLiteral && (Token.LPAREN == c)) {
m_grpCount++;
m_opCount++;
isLiteral = false;
incrementCount();
isAxis = false;
}
else if ((Token.LPAREN == c))
{
nesting++;
if (isLiteral) {
if (!isAxis) {
incrementCount();
}
} else {
m_grpCount++;
incrementCount();
}
isAxis = false;
}

if ((Token.GT == c || Token.LT == c || Token.EQ == c) && Token.EQ != peekNext(pat, i)) {
m_opCount++;
isLiteral = false;
if ((Token.GT == c || Token.LT == c || Token.EQ == c || Token.EM == c)) {
if (Token.EQ != peekNext(pat, i)) {
incrementCount();
}
}
else if ((Token.LPAREN != c) && (Token.RPAREN != c) && (Token.RBRACK != c)) {
m_opCount++;
isLiteral = false;
else if (Token.SLASH == c) {
isAxis = false;
if (Token.SLASH != peekNext(pat, i)) {
incrementCount();
}
}
// '(' and '[' already counted above; ':' is examined in case below
// ',' is part of a function
else if ((Token.LPAREN != c) && (Token.LBRACK != c) && (Token.RPAREN != c)
&& (Token.RBRACK != c) && (Token.COLON != c) && (Token.COMMA != c)) {
if (Token.STAR != c || !isAxis) {
incrementCount();
}
isAxis = false;
}

addToTokenQueue(pat.substring(i, i + 1));
Expand All @@ -376,6 +398,7 @@ else if ((Token.LPAREN != c) && (Token.RPAREN != c) && (Token.RBRACK != c)) {
startSubstring = -1;
posOfNSSep = -1;
m_opCount++;
isAxis = true;
addToTokenQueue(pat.substring(i - 1, i + 1));

break;
Expand All @@ -389,6 +412,9 @@ else if ((Token.LPAREN != c) && (Token.RPAREN != c) && (Token.RBRACK != c)) {
// fall through on purpose
default :
isLiteral = true;
if (!isNum && Token.DOT == c && Token.DOT != peekNext(pat, i)) {
incrementCount();
}
if (-1 == startSubstring)
{
startSubstring = i;
Expand Down Expand Up @@ -443,6 +469,11 @@ else if (null != targetStrings)
m_processor.m_queueMark = 0;
}

private void incrementCount() {
m_opCount++;
isLiteral = false;
}

/**
* Peeks at the next character without advancing the index.
* @param s the input string
Expand Down

1 comment on commit 4b45646

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.