1
0

go.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * Go.
  4. *
  5. * http://golang.org/doc/go_spec.html
  6. *
  7. * TODO: the different string formats have different escape codes, need
  8. * to override the generic filter to handle this
  9. * also, if there's a standard library API list, that would be useful.
  10. *
  11. */
  12. class LuminousGoScanner extends LuminousSimpleScanner {
  13. function type_override($matches) {
  14. $this->record($matches[1], 'IDENT');
  15. $this->record($matches[2], null);
  16. $this->record($matches[3], 'USER_FUNCTION');
  17. $this->pos_shift(strlen($matches[0]));
  18. $this->user_defs[$matches[3]] = ($matches[1] === 'type')? 'TYPE'
  19. : 'FUNCTION';
  20. }
  21. function init() {
  22. $this->add_pattern('COMMENT', LuminousTokenPresets::$C_COMMENT_ML);
  23. $this->add_pattern('COMMENT', LuminousTokenPresets::$C_COMMENT_SL);
  24. $ident = '[\p{L}_][\p{L}\p{N}_]*';
  25. // this should be unicode for letter (\p{L}) and number (\p{N})
  26. $this->add_pattern('type', "/\\b(type|func)(\s+)($ident)/u");
  27. $this->overrides['type'] = array($this, 'type_override');
  28. $this->add_pattern('IDENT', "/$ident/u");
  29. $this->add_pattern('OPERATOR', '/[+\\-\\*\\/%&\\|^<>&=!:\\.,;]+/');
  30. $exp = '[eE][+-]?\d+';
  31. // note the trailing i - which denotes imaginary literals
  32. $this->add_pattern('NUMERIC',
  33. "/(?:\d+\.\d*(?:$exp)?|\d+$exp|\.\d+(?:$exp)?)i?/");
  34. $this->add_pattern('NUMERIC', '/(?:0(?:\d+|x[a-fA-F0-9]+)|\d+)i?/');
  35. $this->add_pattern('CHARACTER',
  36. "/'(?:\\\\(?:\d+|[uUxX][a-fA-F0-9]+|.)|.)'/u");
  37. $this->add_pattern('STRING', LuminousTokenPresets::$DOUBLE_STR);
  38. $this->add_pattern('STRING', '/`(?:[^`\\\\]+|\\\\.)*(?:`|$)/s');
  39. $this->add_identifier_mapping('KEYWORD', array('break', 'case', 'chan',
  40. 'const', 'continue', 'default', 'defer', 'else', 'fallthrough', 'for',
  41. 'func', 'go', 'goto', 'if', 'import', 'interface', 'map', 'package',
  42. 'range', 'return', 'select', 'struct', 'switch', 'type', 'var'));
  43. $this->add_identifier_mapping('TYPE', array('any', 'bool', 'byte',
  44. 'complex', 'complex64', 'complex128', 'int', 'int8', 'int16', 'int32',
  45. 'int64', 'float', 'float32', 'float64', 'string', 'struct',
  46. 'uint', 'uint8', 'uint16', 'uint32', 'uint64', 'uintptr'));
  47. $this->add_identifier_mapping('VALUE', array('false', 'iota', 'true'));
  48. // from the old luminous language file, don't know how sensible these are
  49. $this->add_identifier_mapping('FUNCTION', array('append', 'cap', 'copy',
  50. 'cmplx', 'imag', 'len', 'make', 'new', 'panic', 'print', 'println',
  51. 'real', 'recover', 'sizeof'));
  52. }
  53. public static function guess_language($src, $info) {
  54. $p = 0.0;
  55. if (strpos($src, 'func ') !== false) $p += 0.02;
  56. if (preg_match('/func\s*\\(\s*\w+\s*\\*\s*\w+/', $src)) $p += 0.05;
  57. if (preg_match('/^package\s+\w+/', $src)) $p += 0.01;
  58. if (preg_match('/type\s+\w+\s+struct\s*\\{/', $src)) $p += 0.03;
  59. return $p;
  60. }
  61. }