Skip to content

Commit

Permalink
8285081: Improve XPath operators count accuracy
Browse files Browse the repository at this point in the history
Reviewed-by: naoto, lancea
  • Loading branch information
JoeWang-Java committed Jun 7, 2022
1 parent b12e7f1 commit 8e07839
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

5 comments on commit 8e07839

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 8e07839 Jun 20, 2022

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8e07839 Jun 20, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-8e078391 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 8e078391 from the openjdk/jdk repository.

The commit being backported was authored by Joe Wang on 7 Jun 2022 and was reviewed by Naoto Sato and Lance Andersen.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-8e078391:GoeLin-backport-8e078391
$ git checkout GoeLin-backport-8e078391
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev GoeLin-backport-8e078391

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 8e07839 Jul 15, 2022

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8e07839 Jul 15, 2022

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch GoeLin-backport-8e078391 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 8e078391 from the openjdk/jdk repository.

The commit being backported was authored by Joe Wang on 7 Jun 2022 and was reviewed by Naoto Sato and Lance Andersen.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev GoeLin-backport-8e078391:GoeLin-backport-8e078391
$ git checkout GoeLin-backport-8e078391
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev GoeLin-backport-8e078391

Please sign in to comment.