def test_combined_0(self): lw = latexwalker.LatexWalker(r'\cmd{ab}c*') s = MacroStandardArgsParser('{*[{*') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, len(r'\cmd'), parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='{*[{*', argnlist=[ LatexGroupNode( parsing_state=parsing_state, delimiters=('{', '}'), nodelist=[ LatexCharsNode( parsing_state=parsing_state, chars='ab', pos=5, len=2) ], pos=4, len=4), None, None, LatexCharsNode(parsing_state=parsing_state, chars='c', pos=8, len=1), LatexCharsNode(parsing_state=parsing_state, chars='*', pos=9, len=1) ]))
def test_star_0(self): lw = latexwalker.LatexWalker(r'\cmd xyz') s = MacroStandardArgsParser('*') (argd, p, l) = s.parse_args(lw, len(r'\cmd')) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='*', argnlist=[ None ]) )
def test_marg_1(self): lw = latexwalker.LatexWalker(r'\cmd ab') s = MacroStandardArgsParser('{') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, len(r'\cmd'), parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='{', argnlist=[ LatexCharsNode(parsing_state=parsing_state, chars='a', pos=len(r'\cmd')+1,len=1) ]) )
def parse_args(self, w, pos, parsing_state=None): argspec = '' argnlist = [] origpos = pos while True: # inspect the following token at the given position (skips # spaces if necessary) try: tok = w.get_token(pos) except latexwalker.LatexWalkerEndOfStream: break if tok.tok == 'char' and tok.arg.startswith('*'): argspec += '*' argnlist.append( w.make_node(latexwalker.LatexCharsNode, parsing_state=parsing_state, chars='*', pos=tok.pos, len=1) ) pos = tok.pos + 1 elif tok.tok == 'char' and tok.arg.startswith('['): (node, np, nl) = w.get_latex_maybe_optional_arg( pos, parsing_state=parsing_state ) pos = np + nl argspec += '[' argnlist.append(node) elif tok.tok == 'brace_open': (node, np, nl) = w.get_latex_expression( pos, strict_braces=False, parsing_state=parsing_state, ) pos = np + nl argspec += '{' argnlist.append(node) else: # something else -- we're guessing that it's not a macro # argument break parsed = ParsedMacroArgs( argspec=argspec, argnlist=argnlist, ) return (parsed, origpos, pos-origpos)
def test_oarg_0(self): lw = latexwalker.LatexWalker(r'\cmd[ab] xyz') s = MacroStandardArgsParser('[') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, len(r'\cmd'), parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs(argspec='[', argnlist=[ LatexGroupNode( parsing_state=parsing_state, delimiters=('[', ']'), nodelist=[ LatexCharsNode(parsing_state=parsing_state, chars='ab', pos=5,len=2) ], pos=4,len=4) ]) )
def test_marg_0(self): lw = latexwalker.LatexWalker(r'{ab}') s = MacroStandardArgsParser('{') parsing_state = lw.make_parsing_state() (argd, p, l) = s.parse_args(lw, 0, parsing_state=parsing_state) self.assertPMAEqual( argd, ParsedMacroArgs( argspec='{', argnlist=[ LatexGroupNode( parsing_state=parsing_state, delimiters=('{','}'), nodelist=[ LatexCharsNode(parsing_state=parsing_state, chars='ab', pos=1,len=2) ], pos=0,len=4) ]) )