Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
The `parsing_state` argument was introduced in version 2.0.
"""
if parsing_state is None:
parsing_state = self.make_parsing_state() # get default parsing state
with _PushPropOverride(self, 'strict_braces', strict_braces):
tok = self.get_token(pos, environments=False, parsing_state=parsing_state)
if tok.tok == 'macro':
if tok.arg == 'end':
if not self.tolerant_parsing:
# error, we were expecting a single token
raise LatexWalkerParseError(r"Expected expression, got \end", self.s, pos,
**self.pos_to_lineno_colno(pos, as_dict=True))
else:
return self._mknodeposlen(LatexCharsNode,
parsing_state=parsing_state,
chars='',
pos=tok.pos,
len=0)
return self._mknodeposlen(LatexMacroNode,
parsing_state=parsing_state,
macroname=tok.arg,
nodeargd=None,
macro_post_space=tok.post_space,
nodeoptarg=None, nodeargs=None,
pos=tok.pos, len=tok.len)
if tok.tok == 'specials':
return self._mknodeposlen(LatexSpecialsNode,
return self._mknodeposlen(LatexMacroNode,
parsing_state=parsing_state,
macroname=tok.arg,
nodeoptarg=None,
nodeargs=None,
macro_post_space=tok.post_space,
pos=tok.pos,
len=tok.len)
else:
return self._mknodeposlen(LatexCharsNode,
parsing_state=parsing_state,
chars=tok.arg,
pos=tok.pos,
len=tok.len)
raise LatexWalkerParseError("Unknown token type: {}".format(tok.tok), self.s, pos,
**self.pos_to_lineno_colno(pos, as_dict=True))
# special treatment for \( ... \) and \[ ... \] -- "macros" for
# inline/display math modes
if macro in ['[', ']']:
return LatexToken(tok='mathmode_display', arg='\\'+macro,
pos=pos, len=i, pre_space=space)
if macro in ['(', ')']:
return LatexToken(tok='mathmode_inline', arg='\\'+macro,
pos=pos, len=i, pre_space=space)
# see if we have a begin/end environment
if environments and macro in ['begin', 'end']:
# \begin{environment} or \end{environment}
envmatch = re.match(r'^\s*\{([\w*]+)\}', s[pos+i:])
if envmatch is None:
raise LatexWalkerParseError(
s=s,
pos=pos,
msg=r"Bad \{} macro: expected {{}}".format(macro),
**self.pos_to_lineno_colno(pos, as_dict=True)
)
return LatexToken(
tok=('begin_environment' if macro == 'begin' else 'end_environment'),
arg=envmatch.group(1),
pos=pos,
len=i+envmatch.end(), # !!envmatch.end() counts from pos+i
pre_space=space
)
# get the following whitespace, and store it in the macro's post_space
post_space = ''
**self.pos_to_lineno_colno(tok.pos, as_dict=True)
)
return True
if tok.tok in ('mathmode_inline', 'mathmode_display'):
# see if we need to stop at a math mode
if stop_upon_closing_mathmode is not None:
if tok.arg == stop_upon_closing_mathmode:
# all OK, found the closing mathmode.
return True
if tok.arg in [r'\)', r'\]']:
# this is definitely a closing math-mode delimiter, so
# not a new math mode block. This is a parse error,
# because we need to match the given
# stop_upon_closing_mathmode mode.
raise LatexWalkerParseError(
s=self.s,
pos=tok.pos,
msg="Mismatching closing math mode: '{}', expected '{}'".format(
tok.arg, stop_upon_closing_mathmode,
),
**self.pos_to_lineno_colno(tok.pos, as_dict=True)
)
# all ok, this is a new math mode opening. Keep an assert
# in case we forget to include some math-mode delimiters in
# the future.
assert tok.arg in ['$', '$$', r'\(', r'\[']
elif tok.arg in [r'\)', r'\]']:
# unexpected close-math-mode delimiter, but no
# stop_upon_closing_mathmode was specified. Parse error.
raise LatexWalkerParseError(
s=self.s,
def _get_exc_for_parseerror_or_eof(self, e, tok, what):
"""
(INTERNAL.) Use in an exception handler that captures both
`LatexWalkerEndOfStream` and `LatexWalkerParseError`. Returns what
exception you should raise if you got one of these while parsing, e.g.,
macro arguments.
"""
if not self.tolerant_parsing and isinstance(e, LatexWalkerEndOfStream):
e = LatexWalkerParseError(
s=self.s,
pos=tok.pos,
msg="Missing {}".format(what),
**self.pos_to_lineno_colno(tok.pos, as_dict=True)
)
e.open_contexts.append(
_maketuple('{}'.format(what), tok.pos,
*self.pos_to_lineno_colno(tok.pos))
)
return e