stevedlawrence commented on a change in pull request #214: Sequences and Separators Refactoring and Rewrite URL: https://github.com/apache/incubator-daffodil/pull/214#discussion_r285223159
########## File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/processors/parsers/SeparatedParseHelper.scala ########## @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.daffodil.processors.parsers + +import org.apache.daffodil.processors.ElementRuntimeData +import org.apache.daffodil.exceptions.Assert +import org.apache.daffodil.processors.Failure +import org.apache.daffodil.processors.Success + +sealed abstract class SeparatorParseStatus +object SeparatorParseStatus { + type Type = SeparatorParseStatus + sealed abstract class SeparatorSuccess extends Type + case object SeparatorFound extends SeparatorSuccess + case object SeparatorNotNeeded extends SeparatorSuccess + case object SeparatorFailed extends Type +} + +/** + * Abstracts away separator position. Digests result of parsing + * item and associated separator into a summary status. + */ +sealed abstract class SeparatorParseHelper(protected val sep: Parser, + protected val childParser: Parser, + scParserArg: Separated) + extends Serializable { + + import ParseAttemptStatus._ + + protected val scParser = scParserArg.asInstanceOf[SequenceChildParser with Separated] + + def parseOneWithSeparator(state: PState, requiredOptional: RequiredOptionalStatus): ParseAttemptStatus + + /** + * Sets PState status to failure when separator is not found. + */ + final protected def failedSeparator(pstate: PState, kind: String): Unit = { + val cause = pstate.processorStatus.asInstanceOf[Failure].cause + scParser.trd match { + case erd: ElementRuntimeData if (erd.isArray) => + sep.PE(pstate, "Failed to populate %s[%s]. Missing %s separator. Cause: %s", + erd.prefixedName, pstate.mpstate.arrayPos, kind, cause) + case _ => + sep.PE(pstate, "Failed to parse %s separator. Cause: %s.", kind, cause) + } + } +} + +final class PrefixSeparatorHelper(sep: Parser, childParser: Parser, scParserArg: Separated) + extends SeparatorParseHelper(sep, childParser, scParserArg) + with InfixPrefixSeparatorHelperMixin { + import ParseAttemptStatus._ + + override def kind = "prefix" + + override def parseOneWithSeparator(pstate: PState, requiredOptional: RequiredOptionalStatus): ParseAttemptStatus = + parseOneWithInfixOrPrefixSeparator(true, pstate, requiredOptional) +} + +final class InfixSeparatorHelper(sep: Parser, childParser: Parser, scParserArg: Separated) + extends SeparatorParseHelper(sep, childParser, scParserArg) + with InfixPrefixSeparatorHelperMixin { + import ParseAttemptStatus._ + + override def kind = "infix" + + override def parseOneWithSeparator(pstate: PState, requiredOptional: RequiredOptionalStatus): ParseAttemptStatus = { + + val infixSepShouldBePresent = + pstate.mpstate.groupPos > 1 + + parseOneWithInfixOrPrefixSeparator(infixSepShouldBePresent, pstate, requiredOptional) + } +} + +trait InfixPrefixSeparatorHelperMixin { self: SeparatorParseHelper => + import ParseAttemptStatus._ + + protected def kind: String + + final protected def parseOneWithInfixOrPrefixSeparator(shouldParseTheSep: Boolean, + pstate: PState, + requiredOptional: RequiredOptionalStatus): ParseAttemptStatus = { + + val sepStatus = + if (shouldParseTheSep) { + sep.parse1(pstate) + if (pstate.processorStatus eq Success) + SeparatorParseStatus.SeparatorFound + else + SeparatorParseStatus.SeparatorFailed + } else + SeparatorParseStatus.SeparatorNotNeeded + + val prevBitPosBeforeChild = pstate.bitPos0b + + sepStatus match { + case _: SeparatorParseStatus.SeparatorSuccess => { + val posAfterSep = pstate.bitPos0b + childParser.parse1(pstate) Review comment: Is there a reson we don't need a mark here and can get away with saving/restoring the bitPosition? Isn't there other state that could have changed in childParser.parse1() that would need to get revereted? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
