Skip to content

Commit

Permalink
Refactored PathExpr AST, and started work on AstCreation
Browse files Browse the repository at this point in the history
  • Loading branch information
dvreeze committed Apr 1, 2023
1 parent 44bb38f commit b6d4c1d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
@@ -0,0 +1,49 @@
/*
* Copyright 2011-2017 Chris de Vreeze
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.cdevreeze.xpathparser.ast

/**
* Friendly AST creation API.
*
* @author
* Chris de Vreeze
*/
object AstCreation:

// TODO Implement

// Creating path expressions

val slashOnlyPathExpr: PathExpr = SlashOnlyPathExpr

def singleSlashPathExpr(
firstStep: StepExpr,
remainder: SingleStepNestedPathExpr*
): PathExpr =
PathExprStartingWithSingleSlash(RelativePathExpr(firstStep, remainder.toIndexedSeq))

def doubleSlashPathExpr(
firstStep: StepExpr,
remainder: SingleStepNestedPathExpr*
): PathExpr =
PathExprStartingWithDoubleSlash(RelativePathExpr(firstStep, remainder.toIndexedSeq))

def relativePathExpr(
firstStep: StepExpr,
remainder: SingleStepNestedPathExpr*
): PathExpr =
RelativePathExpr(firstStep, remainder.toIndexedSeq)
20 changes: 12 additions & 8 deletions shared/src/main/scala/eu/cdevreeze/xpathparser/ast/XPathElem.scala
Expand Up @@ -522,19 +522,23 @@ sealed trait RelativePathExpr extends PathExpr

sealed trait SimpleRelativePathExpr extends RelativePathExpr

final case class CompoundRelativePathExpr(init: RelativePathExpr, op: StepOp, lastStepExpr: StepExpr)
/**
* A step expression preceded by a single or double slash. Not a known concept in the XPath 3.1 grammar.
*/
final case class SingleStepNestedPathExpr(op: StepOp, stepExpr: StepExpr) extends XPathElem:

def children: IndexedSeq[XPathElem] = IndexedSeq(op, stepExpr)

final case class CompoundRelativePathExpr(firstStep: StepExpr, remainder: IndexedSeq[SingleStepNestedPathExpr])
extends RelativePathExpr:

def children: IndexedSeq[XPathElem] = IndexedSeq(init, op, lastStepExpr)
def children: IndexedSeq[XPathElem] = remainder.prepended(firstStep)

object RelativePathExpr:

def apply(firstExpr: StepExpr, operatorExprPairs: IndexedSeq[(StepOp, StepExpr)]): RelativePathExpr =
if operatorExprPairs.isEmpty then firstExpr
else
val (lastOp, lastExpr) = operatorExprPairs.last
// Recursive call
CompoundRelativePathExpr(apply(firstExpr, operatorExprPairs.init), lastOp, lastExpr)
def apply(firstExpr: StepExpr, remainder: IndexedSeq[SingleStepNestedPathExpr]): RelativePathExpr =
if remainder.isEmpty then firstExpr
else CompoundRelativePathExpr(firstExpr, remainder)

/**
* Single step in an absolute or relative path expression. Note that step expressions are either postfix expressions or
Expand Down
Expand Up @@ -305,7 +305,10 @@ object XPathElemParser:
val relativePathExpr: P[RelativePathExpr] = P.defer {
(stepExpr.skipWS.soft ~ ((DT.slash | DT.doubleSlash).skipWS.string ~ stepExpr.skipWS).rep0).map {
case (firstExp, opExpPairs) =>
RelativePathExpr(firstExp, opExpPairs.toIndexedSeq.map(kv => StepOp.parse(kv._1) -> kv._2))
RelativePathExpr(
firstExp,
opExpPairs.toIndexedSeq.map(kv => SingleStepNestedPathExpr(StepOp.parse(kv._1), kv._2))
)
}
}

Expand Down Expand Up @@ -366,7 +369,6 @@ object XPathElemParser:
((NDT.childWord | NDT.descendantWord | NDT.attributeWord | NDT.selfWord | NDT.descendantOrSelfWord |
NDT.followingSiblingWord | NDT.followingWord | NDT.namespaceWord).skipWS.string.soft <* DT.doubleColon.skipWS)
.map {

case "child" => ForwardAxis.Child
case "descendant" => ForwardAxis.Descendant
case "attribute" => ForwardAxis.Attribute
Expand Down

0 comments on commit b6d4c1d

Please sign in to comment.