Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question has already been answered and this is slightly off-topic, but...</p> <p>If you want to parse code, it is often easier and less error-prone to use a parser. If your code is Python code, Python comes with a couple of parsers (<a href="http://docs.python.org/library/tokenize.html" rel="nofollow noreferrer">tokenize</a>, <a href="http://docs.python.org/library/ast.html" rel="nofollow noreferrer">ast</a>, <a href="http://docs.python.org/library/parser.html#module-parser" rel="nofollow noreferrer">parser</a>). For other languages, you can find a lot of parsers on the internet. <a href="http://www.antlr.org/" rel="nofollow noreferrer">ANTRL</a> is a well-known one with Python <a href="http://www.antlr.org/wiki/display/ANTLR3/Example" rel="nofollow noreferrer">bindings</a>.</p> <p>As an example, the following couple of lines of code print all lines of a Python module that are not comments and not doc-strings:</p> <pre><code>import tokenize ignored_tokens = [tokenize.NEWLINE,tokenize.COMMENT,tokenize.N_TOKENS ,tokenize.STRING,tokenize.ENDMARKER,tokenize.INDENT ,tokenize.DEDENT,tokenize.NL] with open('test.py', 'r') as f: g = tokenize.generate_tokens(f.readline) line_num = 0 for a_token in g: if a_token[2][0] != line_num and a_token[0] not in ignored_tokens: line_num = a_token[2][0] print(a_token) </code></pre> <p>As <code>a_token</code> above is already parsed, you can easily check for function definition, too. You can also keep track where the function ends by looking at the current column start <code>a_token[2][1]</code>. If you want to do more complex things, you should use ast.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload