コード例 #1
0
    def _apply_strategy(self, **kwargs):
        if not self._text.startswith(ARG_SYM, self._next_index):
            self.error_msg = \
                "Invalid token. Expected an argument declaration there " + \
                "(starting with '" + ARG_SYM + "')."
            return False
        self._next_index += 1
        self._update_furthest_matched_index()
        self._tokens.append(
            LexicalToken(TerminalType.arg_marker, ARG_SYM)
        )
        
        arg_name = extract_identifier(self._text, self._next_index)
        if arg_name is None:
            self.error_msg = \
                "Didn't expect the line to end there. Expected an argument name."
            return False
        elif len(arg_name) == 0:
            self.error_msg = \
                "Couldn't extract the argument name. Arguments must have a name."
            return False
        self._next_index += len(arg_name)
        self._update_furthest_matched_index()
        self._tokens.append(LexicalToken(TerminalType.arg_name, arg_name))

        return True
コード例 #2
0
ファイル: rule_variation.py プロジェクト: SimGus/Chatette
    def _apply_strategy(self, **kwargs):
        if not self._text.startswith(VARIATION_SYM, self._next_index):
            self.error_msg = \
                "Invalid token. Expected a variation there (starting with '" + \
                VARIATION_SYM + "')."
            return False
        self._next_index += 1
        self._update_furthest_matched_index()
        self._tokens.append(
            LexicalToken(TerminalType.variation_marker, VARIATION_SYM))

        variation_name = extract_identifier(self._text, self._next_index)
        if variation_name is None:
            self.error_msg = \
                "Didn't expect an end of line there. Expected a variation name."
            return False
        elif len(variation_name) == 0:
            self.error_msg = \
                "Couldn't extract the name of the variation. Variation names " + \
                "must be at least one character long."
            return False

        self._next_index += len(variation_name)
        self._update_furthest_matched_index()
        self._tokens.append(
            LexicalToken(TerminalType.variation_name, variation_name))

        return True
コード例 #3
0
    def _apply_strategy(self, **kwargs):
        unit_start_rule = RuleUnitStart(self._text, self._next_index)
        if not unit_start_rule.matches(extracting_decl=False):
            self.error_msg = unit_start_rule.error_msg
            self._update_furthest_matched_index(unit_start_rule)
            return False
        self._next_index = unit_start_rule.get_next_index_to_match()
        self._update_furthest_matched_index(unit_start_rule)
        self._tokens.extend(unit_start_rule.get_lexical_tokens())

        if self._text.startswith(CASE_GEN_SYM, self._next_index):
            self._tokens.append(
                LexicalToken(TerminalType.casegen_marker, CASE_GEN_SYM)
            )
            self._next_index += 1
            self._update_furthest_matched_index()

        identifier = extract_identifier(self._text, self._next_index)
        if identifier is not None:
            self._tokens.append(
                LexicalToken(TerminalType.unit_identifier, identifier)
            )
            self._next_index += len(identifier)
            self._update_furthest_matched_index()

        if not self._match_any_order(
            [None, RuleVariation, RuleRandGen, RuleArgAssignment]
        ):
            return False

        if not self._text.startswith(UNIT_END_SYM, self._next_index):
            self.error_msg = \
                "Invalid token. Expected the unit reference to end here (" + \
                "using character '" + UNIT_END_SYM + "')."
            return False

        # TODO maybe making a function for this would be useful
        if self._tokens[0].type == TerminalType.alias_ref_start:
            unit_end_type = TerminalType.alias_ref_end
        elif self._tokens[0].type == TerminalType.slot_ref_start:
            unit_end_type = TerminalType.slot_ref_end
        elif self._tokens[0].type == TerminalType.intent_ref_start:
            unit_end_type = TerminalType.intent_ref_end
        else:  # Should never happen
            raise ValueError(
                "An unexpected error happened during parsing: tried to " + \
                "parse the end of a unit but couldn't find its start in " + \
                "the previously parsed data.\nData was: " + str(self._tokens)
            )

        self._next_index += 1
        self._update_furthest_matched_index()
        self._tokens.append(LexicalToken(unit_end_type, UNIT_END_SYM))

        return True
コード例 #4
0
ファイル: rule_rand_gen.py プロジェクト: lei1993/oa_qa
    def _apply_strategy(self, **kwargs):
        if not self._text.startswith(RAND_GEN_SYM, self._next_index):
            self.error_msg = \
                "Invalid token. Expected a random generation modifier to " + \
                "begin there (starting with '" + RAND_GEN_SYM + "')."
            return False
        self._next_index += 1
        self._update_furthest_matched_index()
        self._tokens.append(
            LexicalToken(TerminalType.randgen_marker, RAND_GEN_SYM))

        if self._text.startswith(RAND_GEN_OPPOSITE_SYM, self._next_index):
            self._next_index += 1
            self._update_furthest_matched_index()
            self._tokens.append(
                LexicalToken(TerminalType.opposite_randgen_marker,
                             RAND_GEN_OPPOSITE_SYM))

        # TODO not sure `extract_identifier` is the best thing to use here
        randgen_name = extract_identifier(self._text, self._next_index)
        if randgen_name is None:
            self.error_msg = \
                "Didn't expect the line to end there. Expected a name for " + \
                "the random generation modifier, a percentage for it or " + \
                "the end of the unit or choice."
            return False
        if len(randgen_name) > 0:
            self._next_index += len(randgen_name)
            self._update_furthest_matched_index()
            self._tokens.append(
                LexicalToken(TerminalType.randgen_name, randgen_name))

        if self._text.startswith(RAND_GEN_PERCENT_SYM, self._next_index):
            self._next_index += 1
            self._update_furthest_matched_index()
            self._tokens.append(
                LexicalToken(TerminalType.percentgen_marker,
                             RAND_GEN_PERCENT_SYM))

            if not self._try_to_match_rule(RulePercentGen):
                self.error_msg += \
                    " Percentage for the random generation is required after " + \
                    "its marker character ('" + RAND_GEN_PERCENT_SYM + "')."
                return False

        return True
コード例 #5
0
ファイル: rule_unit_decl.py プロジェクト: lei1993/oa_qa
    def _apply_strategy(self, **kwargs):
        if not self._try_to_match_rule(RuleUnitStart):
            return False

        if self._text.startswith(CASE_GEN_SYM, self._next_index):
            self._next_index += 1
            self._update_furthest_matched_index()
            self._tokens.append(
                LexicalToken(TerminalType.casegen_marker, CASE_GEN_SYM))

        identifier = extract_identifier(self._text, self._next_index)
        if identifier is not None:
            self._next_index += len(identifier)
            self._update_furthest_matched_index()
            self._tokens.append(
                LexicalToken(TerminalType.unit_identifier, identifier))

        if not self._match_any_order([None, RuleArgDecl, RuleVariation]):
            return False

        if not self._text.startswith(UNIT_END_SYM, self._next_index):
            self.error_msg = \
                "Invalid token. Expected the end of the unit declaration " + \
                "there (using symbol '" + UNIT_END_SYM + "')."
            return False

        # TODO maybe making a function for this would be useful
        if self._tokens[0].type == TerminalType.alias_decl_start:
            unit_end_type = TerminalType.alias_decl_end
        elif self._tokens[0].type == TerminalType.slot_decl_start:
            unit_end_type = TerminalType.slot_decl_end
        elif self._tokens[0].type == TerminalType.intent_decl_start:
            unit_end_type = TerminalType.intent_decl_end
        else:  # Should never happen
            raise ValueError(
                "An unexpected error happened during parsing: tried to " + \
                "parse the end of a unit but couldn't find its start in " + \
                "the previously parsed data.\nData was: " + str(self._tokens)
            )

        self._next_index += 1
        self._update_furthest_matched_index()
        self._tokens.append(LexicalToken(unit_end_type, UNIT_END_SYM))

        return True