示例#1
0
    def generate_code(self, pos_name="pos"):
        self.parser.assign_wrapper(self)

        return copy_with(
            self.parser.gen_code_or_fncall(pos_name),
            res_var_name=self.parser.allargs[self.index]
        )
示例#2
0
    def generate_code(self, pos_name="pos"):

        # The sub-parser result will not be used.  We have to notify it if it's
        # a Row so it does not try to generate an anonymous row type.
        if isinstance(self.parser, Row):
            self.parser.assign_wrapper(self)

        self.enum_type_inst.add_to_context()

        parser_context = (
            copy(self.parser.gen_code_or_fncall(pos_name))
            if self.parser
            else ParserCodeContext(
                pos_var_name=pos_name,
                res_var_name="",
                code="",
                var_defs=[]
            )
        )

        env = TemplateEnvironment(
            _self=self,
            res=gen_name("enum_res"),
            parser_context=parser_context

        )

        return copy_with(
            parser_context,
            res_var_name=env.res,
            code=render('parsers/enum_code_ada', env),
            var_defs=parser_context.var_defs + [(env.res, self.get_type())]
        )
示例#3
0
    def as_bool(self, dest=None):
        """
        Returns the self parser, modified to return a bool rather than the
        sub-parser result. The result will be true if the parse was
        successful, false otherwise.

        This is typically useful to store specific tokens as attributes,
        for example in Ada, you'll mark a subprogram as overriding with the
        "overriding" keyword, and we want to store that in the tree as a
        boolean attribute, so we'll use::

            Opt("overriding").as_bool()

        :param CompiledType|None dest: If not None, then it is expected that
            this is an EnumType with qualifier = True. In this cases, the
            result will be booleanized.

        :rtype: Opt
        """
        if dest is None:
            base, alt_true, alt_false = (BoolType, BoolType, BoolType)
        else:
            base = assert_type(dest, ASTNode)
            assert base.is_bool_node
            alt_true, alt_false = base._alternatives

        new = copy_with(self, _booleanize=(base, alt_true, alt_false))
        return new
示例#4
0
    def generate_code(self, pos_name="pos"):

        if isinstance(self.parser, Row):
            self.parser.assign_wrapper(self)

        self.typ.add_to_context()

        parser_context = self.parser.gen_code_or_fncall(pos_name)
        ":type: ParserCodeContext"

        t_env = TemplateEnvironment(
            _self=self,
            # The template needs the compiler context to retrieve the types of
            # the tree fields (required by get_types()).
            parser_context=parser_context,
            args=(
                self.parser.args
                if isinstance(self.parser, Row) else
                [parser_context.res_var_name]
            ),
            res=gen_name("transform_res"),
        )

        return copy_with(
            parser_context,
            res_var_name=t_env.res,
            var_defs=parser_context.var_defs + [
                (t_env.res, self.get_type()),
            ],
            code=render('parsers/transform_code_ada', t_env, pos_name=pos_name)
        )
示例#5
0
    def generate_code(self, pos_name="pos"):

        self.typ.add_to_context()

        parser_context = self.parser.gen_code_or_fncall(pos_name)
        ":type: ParserCodeContext"

        t_env = TemplateEnvironment(
            parser=self,
            # The template needs the compiler context to retrieve the types of
            # the tree fields (required by get_types()).
            parser_context=parser_context,
            args=(self.parser.args if isinstance(self.parser, Row) else
                  [parser_context.res_var_name]),
            res=gen_name("transform_res"),
        )

        return copy_with(parser_context,
                         res_var_name=t_env.res,
                         var_defs=parser_context.var_defs + [
                             (t_env.res, self.get_type()),
                         ],
                         code=render('parsers/transform_code_ada',
                                     t_env,
                                     pos_name=pos_name))
示例#6
0
    def generate_code(self, pos_name="pos"):
        parser_context = copy(self.parser.gen_code_or_fncall(pos_name))

        t_env = TemplateEnvironment(pos_name=pos_name,
                                    parser=self,
                                    bool_res=gen_name("opt_bool_res"),
                                    parser_context=parser_context)

        return copy_with(
            parser_context,
            code=render('parsers/opt_code_ada', t_env),
            res_var_name=(t_env.bool_res if self._booleanize else
                          parser_context.res_var_name),
            var_defs=(parser_context.var_defs +
                      ([(t_env.bool_res,
                         self._booleanize[0])] if self._booleanize else [])))
示例#7
0
    def error(self):
        """
        Returns the self parser, modified to function as an error recovery
        parser.

        The semantic of Opt in this case is that it will try to parse it's
        sub parser, and when failing, it will add a diagnostic to the
        parser's diagnostic list.

        NOTE: There is no diagnostics backtracking if the parent parser is
        discarded. That means that you should only use this parser in cases
        in which you are sure that you are in a successfull branch of your
        parser. This is neither checked statically nor dynamically so use
        with care!

        :rtype: Opt
        """
        return copy_with(self, _is_error=True)
示例#8
0
    def error(self):
        """
        Returns the self parser, modified to function as an error recovery
        parser.

        The semantic of Opt in this case is that it will try to parse it's
        sub parser, and when failing, it will add a diagnostic to the
        parser's diagnostic list.

        NOTE: There is no diagnostics backtracking if the parent parser is
        discarded. That means that you should only use this parser in cases
        in which you are sure that you are in a successfull branch of your
        parser. This is neither checked statically nor dynamically so use
        with care!

        :rtype: Opt
        """
        return copy_with(self, _is_error=True)
示例#9
0
    def generate_code(self, pos_name="pos"):

        self.enum_type_inst.add_to_context()

        parser_context = (
            copy(self.parser.gen_code_or_fncall(pos_name))
            if self.parser else ParserCodeContext(
                pos_var_name=pos_name, res_var_name="", code="", var_defs=[]))

        env = TemplateEnvironment(parser=self,
                                  res=gen_name("enum_res"),
                                  parser_context=parser_context)

        return copy_with(parser_context,
                         res_var_name=env.res,
                         code=render('parsers/enum_code_ada', env),
                         var_defs=parser_context.var_defs +
                         [(env.res, self.get_type())])
示例#10
0
    def generate_code(self, pos_name="pos"):
        parser_context = copy(
            self.parser.gen_code_or_fncall(pos_name)
        )

        t_env = TemplateEnvironment(
            pos_name=pos_name,
            _self=self,
            bool_res=gen_name("opt_bool_res"),
            parser_context=parser_context
        )

        return copy_with(
            parser_context,
            code=render('parsers/opt_code_ada', t_env),
            res_var_name=(t_env.bool_res if self._booleanize
                          else parser_context.res_var_name),
            var_defs=parser_context.var_defs + ([(t_env.bool_res, BoolType)]
                                                if self._booleanize else [])
        )
示例#11
0
    def as_bool(self):
        """
        Returns the self parser, modified to return a bool rather than the
        sub-parser result. The result will be true if the parse was
        successful, false otherwise.

        This is typically useful to store specific tokens as attributes,
        for example in Ada, you'll mark a subprogram as overriding with the
        "overriding" keyword, and we want to store that in the tree as a
        boolean attribute, so we'll use::

            Opt("overriding").as_bool()

        :rtype: Opt
        """
        new = copy_with(self, _booleanize=True)
        if new.contains_anonymous_row:
            # What the sub-parser will match will not be returned, so there is
            # no need to generate an anonymous row type.  Tell so to the
            # Row sub-parser.
            assert isinstance(new.parser, Row)
            new.parser.assign_wrapper(new)
        return new
示例#12
0
 def generate_code(self, pos_name="pos"):
     return copy_with(self.parser.gen_code_or_fncall(pos_name),
                      res_var_name=self.parser.allargs[self.index])