pylatexenc/latexwalker.py
def get_latex_expression(self, pos, strict_braces=None):
        """
        Parses the latex content given to the constructor (and stored in `self.s`),
        starting at position `pos`, to parse a single LaTeX expression.

        Reads a latex expression, e.g. macro argument. This may be a single char, an escape
        sequence, or a expression placed in braces.  This is what TeX calls a "token" (and
        not what we call a token... anyway).

        Returns a tuple `(node, pos, len)`, where `pos` is the position of the
        first char of the expression and `len` the length of the expression.
        """

        with _PushPropOverride(self, 'strict_braces', strict_braces):

            tok = self.get_token(pos, environments=False, keep_inline_math=False)

            if (tok.tok == 'macro'):
                if (tok.arg == 'end'):
                    if not self.tolerant_parsing:
                        # error, this should be an \end{environment}, not an argument in itself
                        raise LatexWalkerParseError("Expected expression, got \end", self.s, pos)
                    else:
                        return (LatexCharsNode(chars=''), tok.pos, 0)
                return (LatexMacroNode(macroname=tok.arg, nodeoptarg=None, nodeargs=[],
                                       macro_post_space=tok.post_space),
                        tok.pos, tok.len)
            if (tok.tok == 'comment'):
                return self.get_latex_expression(pos+tok.len)
            if (tok.tok == 'brace_open'):
                return self.get_latex_braced_group(tok.pos)
            if (tok.tok == 'brace_close'):
                if (self.strict_braces and not self.tolerant_parsing):
                    raise LatexWalkerParseError("Expected expression, got closing brace!", self.s, pos)
                return (LatexCharsNode(chars=''), tok.pos, 0)
            if (tok.tok == 'char'):
                return (LatexCharsNode(chars=tok.arg), tok.pos, tok.len)

            raise LatexWalkerParseError("Unknown token type: %s" %(tok.tok), self.s, pos)
def get_latex_expression(self, pos, strict_braces=None):
        """
        Parses the latex content given to the constructor (and stored in `self.s`),
        starting at position `pos`, to parse a single LaTeX expression.

        Reads a latex expression, e.g. macro argument. This may be a single char, an escape
        sequence, or a expression placed in braces.  This is what TeX calls a "token" (and
        not what we call a token... anyway).

        Returns a tuple `(node, pos, len)`, where `pos` is the position of the
        first char of the expression and `len` the length of the expression.
        """

        with _PushPropOverride(self, 'strict_braces', strict_braces):

            tok = self.get_token(pos, environments=False, keep_inline_math=False)

            if (tok.tok == 'macro'):
                if (tok.arg == 'end'):
                    if not self.tolerant_parsing:
                        # error, this should be an \end{environment}, not an argument in itself
                        raise LatexWalkerParseError("Expected expression, got \end", self.s, pos)
                    else:
                        return (LatexCharsNode(chars=''), tok.pos, 0)
                return (LatexMacroNode(macroname=tok.arg, nodeoptarg=None, nodeargs=[],
                                       macro_post_space=tok.post_space),
                        tok.pos, tok.len)
            if (tok.tok == 'comment'):
                return self.get_latex_expression(pos+tok.len)
            if (tok.tok == 'brace_open'):
                return self.get_latex_braced_group(tok.pos)
            if (tok.tok == 'brace_close'):
                if (self.strict_braces and not self.tolerant_parsing):
                    raise LatexWalkerParseError("Expected expression, got closing brace!", self.s, pos)
                return (LatexCharsNode(chars=''), tok.pos, 0)
            if (tok.tok == 'char'):
                return (LatexCharsNode(chars=tok.arg), tok.pos, tok.len)

            raise LatexWalkerParseError("Unknown token type: %s" %(tok.tok), self.s, pos)
def get_latex_expression ( self , pos , strict_braces = None ) : with _PushPropOverride ( self , 'strict_braces' , strict_braces ) : tok = self . get_token ( pos , environments = False , keep_inline_math = False ) if ( tok . tok == 'macro' ) : if ( tok . arg == 'end' ) : if not self . tolerant_parsing : # error, this should be an \end{environment}, not an argument in itself raise LatexWalkerParseError ( "Expected expression, got \end" , self . s , pos ) else : return ( LatexCharsNode ( chars = '' ) , tok . pos , 0 ) return ( LatexMacroNode ( macroname = tok . arg , nodeoptarg = None , nodeargs = [ ] , macro_post_space = tok . post_space ) , tok . pos , tok . len ) if ( tok . tok == 'comment' ) : return self . get_latex_expression ( pos + tok . len ) if ( tok . tok == 'brace_open' ) : return self . get_latex_braced_group ( tok . pos ) if ( tok . tok == 'brace_close' ) : if ( self . strict_braces and not self . tolerant_parsing ) : raise LatexWalkerParseError ( "Expected expression, got closing brace!" , self . s , pos ) return ( LatexCharsNode ( chars = '' ) , tok . pos , 0 ) if ( tok . tok == 'char' ) : return ( LatexCharsNode ( chars = tok . arg ) , tok . pos , tok . len ) raise LatexWalkerParseError ( "Unknown token type: %s" % ( tok . tok ) , self . s , pos )
Parses the latex content given to the constructor (and stored in `self.s`),
        starting at position `pos`, to parse a single LaTeX expression.

        Reads a latex expression, e.g. macro argument. This may be a single char, an escape
        sequence, or a expression placed in braces.  This is what TeX calls a "token" (and
        not what we call a token... anyway).

        Returns a tuple `(node, pos, len)`, where `pos` is the position of the
        first char of the expression and `len` the length of the expression.
Parses the latex content given to the constructor ( and stored in self . s ) starting at position pos to parse a single LaTeX expression .