1  <?php
 2  /**
 3   * Python Scanner
 4   * Part of cYcnus.CodeBB - visit http://rd.cYcnus.de
 5   * Version 1.0:beta2 - 2004-11-10
 6   */
 7
 8  require_once 'scanner.php';
 9
10  define('DEF_EXPECTED', 101);
11  define('CLASS_EXPECTED', 102);
12
13  class Scanner_python extends Scanner {
14    var $language = 'Python';
15    var $lang = 'python';
16    var $generation = '1.0';
17    var $version = 'beta';
18    var $author = 'cYcnus';
19
20    var $status = 0; #ZERO
21
22    function Scanner_python() {
23      $this->reserved_words = new KeywordList(array(
24        'break', 'continue', 'del', 'except', 'exec', 'finally', 'pass', 'print',
25        'raise', 'return', 'try', 'global', 'assert', 'lambda', 'yield', 'def',
26        'class', 'for', 'while', 'if', 'elif', 'else', 'and', 'in', 'is', 'not',
27        'or', 'import', 'from'
28      ));
29    }
30
31    function next_token() {
32      $this->token_kind = toError;
33      $code = $this->code;
34      $pos = $this->pos;
35
36      if (preg_match('/\s+/sA', $code, $res, 0, $pos))
37        $this->token_kind = toSpace;
38      elseif ($this->status >= DEF_EXPECTED) {
39        if (preg_match('/[A-Za-z_][A-Za-z_0-9]*/sA', $code, $res, 0, $pos))
40          $this->token_kind = ($this->status == DEF_EXPECTED ? toFunction : toClass);
41        elseif (!preg_match('/./sA', $code, $res, 0, $pos))
42          return false;
43        $this->status = 0;
44      }
45      elseif (preg_match('/[uU]?[rR]?(?:"""[^"\\\\]*(?:(?:\\\\.|(?!""")")[^"\\\\]*)*"?"?"?|\'\'\'[^\'\\\\]*(?:(?:\\\\.|(?!\'\'\')\')[^\'\\\\]*)*\'?\'?\'?)/sA', $code, $res, 0, $pos))
46        $this->token_kind = toDocString;
47      elseif (preg_match('/[uU]?[rR]?("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"?|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'?)/sA', $code, $res, 0, $pos))
48        $this->token_kind = toString;
49      elseif (preg_match('/[A-Za-z_][A-Za-z_0-9]*/sA', $code, $res, 0, $pos)) {
50        if ($this->reserved_words->contain($res[0])) {
51          $this->token_kind = toReserved;
52          if ($res[0] == 'def')
53            $this->status = DEF_EXPECTED;
54          elseif ($res[0] == 'class')
55            $this->status = CLASS_EXPECTED;
56        }
57        else
58          $this->token_kind = toIdent;
59      }
60      elseif (preg_match('/#[^\n]*/sA', $code, $res, 0, $pos))
61        $this->token_kind = toComment;
62      elseif (preg_match('/[-+*\/%=<>,;().:[\]{}\\\\]/sA', $code, $res, 0, $pos))
63        $this->token_kind = toOperator;
64      elseif (preg_match('/\d+(?:[eE][+-]?\d+|(?:\.\d*(?:[eE][+-]?\d+)?))[jJ]?/sA', $code, $res, 0, $pos))
65        $this->token_kind = toFloat;
66      elseif (preg_match('/0x[0-9A-Fa-f][lLjJ]?/sA', $code, $res, 0, $pos))
67        $this->token_kind = toHex;
68      elseif (preg_match('/\d+[lLjJ]?/sA', $code, $res, 0, $pos))
69        $this->token_kind = toInteger;
70      elseif (preg_match('/`/sA', $code, $res, 0, $pos)) # i'm not sure what to do here.
71        $this->token_kind = toInterpreted;
72
73      elseif (!preg_match('/./sA', $code, $res, 0, $pos))
74        return false;
75
76      $this->set_token($res[0]);
77      return true;
78    }
79  }
80  ?>