You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

erlang.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package e
  2. import (
  3. . "github.com/alecthomas/chroma" // nolint
  4. "github.com/alecthomas/chroma/lexers/internal"
  5. )
  6. // Erlang lexer.
  7. var Erlang = internal.Register(MustNewLexer(
  8. &Config{
  9. Name: "Erlang",
  10. Aliases: []string{"erlang"},
  11. Filenames: []string{"*.erl", "*.hrl", "*.es", "*.escript"},
  12. MimeTypes: []string{"text/x-erlang"},
  13. },
  14. Rules{
  15. "root": {
  16. {`\s+`, Text, nil},
  17. {`%.*\n`, Comment, nil},
  18. {Words(``, `\b`, `after`, `begin`, `case`, `catch`, `cond`, `end`, `fun`, `if`, `let`, `of`, `query`, `receive`, `try`, `when`), Keyword, nil},
  19. {Words(``, `\b`, `abs`, `append_element`, `apply`, `atom_to_list`, `binary_to_list`, `bitstring_to_list`, `binary_to_term`, `bit_size`, `bump_reductions`, `byte_size`, `cancel_timer`, `check_process_code`, `delete_module`, `demonitor`, `disconnect_node`, `display`, `element`, `erase`, `exit`, `float`, `float_to_list`, `fun_info`, `fun_to_list`, `function_exported`, `garbage_collect`, `get`, `get_keys`, `group_leader`, `hash`, `hd`, `integer_to_list`, `iolist_to_binary`, `iolist_size`, `is_atom`, `is_binary`, `is_bitstring`, `is_boolean`, `is_builtin`, `is_float`, `is_function`, `is_integer`, `is_list`, `is_number`, `is_pid`, `is_port`, `is_process_alive`, `is_record`, `is_reference`, `is_tuple`, `length`, `link`, `list_to_atom`, `list_to_binary`, `list_to_bitstring`, `list_to_existing_atom`, `list_to_float`, `list_to_integer`, `list_to_pid`, `list_to_tuple`, `load_module`, `localtime_to_universaltime`, `make_tuple`, `md5`, `md5_final`, `md5_update`, `memory`, `module_loaded`, `monitor`, `monitor_node`, `node`, `nodes`, `open_port`, `phash`, `phash2`, `pid_to_list`, `port_close`, `port_command`, `port_connect`, `port_control`, `port_call`, `port_info`, `port_to_list`, `process_display`, `process_flag`, `process_info`, `purge_module`, `put`, `read_timer`, `ref_to_list`, `register`, `resume_process`, `round`, `send`, `send_after`, `send_nosuspend`, `set_cookie`, `setelement`, `size`, `spawn`, `spawn_link`, `spawn_monitor`, `spawn_opt`, `split_binary`, `start_timer`, `statistics`, `suspend_process`, `system_flag`, `system_info`, `system_monitor`, `system_profile`, `term_to_binary`, `tl`, `trace`, `trace_delivered`, `trace_info`, `trace_pattern`, `trunc`, `tuple_size`, `tuple_to_list`, `universaltime_to_localtime`, `unlink`, `unregister`, `whereis`), NameBuiltin, nil},
  20. {Words(``, `\b`, `and`, `andalso`, `band`, `bnot`, `bor`, `bsl`, `bsr`, `bxor`, `div`, `not`, `or`, `orelse`, `rem`, `xor`), OperatorWord, nil},
  21. {`^-`, Punctuation, Push("directive")},
  22. {`(\+\+?|--?|\*|/|<|>|/=|=:=|=/=|=<|>=|==?|<-|!|\?)`, Operator, nil},
  23. {`"`, LiteralString, Push("string")},
  24. {`<<`, NameLabel, nil},
  25. {`>>`, NameLabel, nil},
  26. {`((?:[a-z]\w*|'[^\n']*[^\\]'))(:)`, ByGroups(NameNamespace, Punctuation), nil},
  27. {`(?:^|(?<=:))((?:[a-z]\w*|'[^\n']*[^\\]'))(\s*)(\()`, ByGroups(NameFunction, Text, Punctuation), nil},
  28. {`[+-]?(?:[2-9]|[12][0-9]|3[0-6])#[0-9a-zA-Z]+`, LiteralNumberInteger, nil},
  29. {`[+-]?\d+`, LiteralNumberInteger, nil},
  30. {`[+-]?\d+.\d+`, LiteralNumberFloat, nil},
  31. {`[]\[:_@\".{}()|;,]`, Punctuation, nil},
  32. {`(?:[A-Z_]\w*)`, NameVariable, nil},
  33. {`(?:[a-z]\w*|'[^\n']*[^\\]')`, Name, nil},
  34. {`\?(?:(?:[A-Z_]\w*)|(?:[a-z]\w*|'[^\n']*[^\\]'))`, NameConstant, nil},
  35. {`\$(?:(?:\\(?:[bdefnrstv\'"\\]|[0-7][0-7]?[0-7]?|(?:x[0-9a-fA-F]{2}|x\{[0-9a-fA-F]+\})|\^[a-zA-Z]))|\\[ %]|[^\\])`, LiteralStringChar, nil},
  36. {`#(?:[a-z]\w*|'[^\n']*[^\\]')(:?\.(?:[a-z]\w*|'[^\n']*[^\\]'))?`, NameLabel, nil},
  37. {`\A#!.+\n`, CommentHashbang, nil},
  38. {`#\{`, Punctuation, Push("map_key")},
  39. },
  40. "string": {
  41. {`(?:\\(?:[bdefnrstv\'"\\]|[0-7][0-7]?[0-7]?|(?:x[0-9a-fA-F]{2}|x\{[0-9a-fA-F]+\})|\^[a-zA-Z]))`, LiteralStringEscape, nil},
  42. {`"`, LiteralString, Pop(1)},
  43. {`~[0-9.*]*[~#+BPWXb-ginpswx]`, LiteralStringInterpol, nil},
  44. {`[^"\\~]+`, LiteralString, nil},
  45. {`~`, LiteralString, nil},
  46. },
  47. "directive": {
  48. {`(define)(\s*)(\()((?:(?:[A-Z_]\w*)|(?:[a-z]\w*|'[^\n']*[^\\]')))`, ByGroups(NameEntity, Text, Punctuation, NameConstant), Pop(1)},
  49. {`(record)(\s*)(\()((?:(?:[A-Z_]\w*)|(?:[a-z]\w*|'[^\n']*[^\\]')))`, ByGroups(NameEntity, Text, Punctuation, NameLabel), Pop(1)},
  50. {`(?:[a-z]\w*|'[^\n']*[^\\]')`, NameEntity, Pop(1)},
  51. },
  52. "map_key": {
  53. Include("root"),
  54. {`=>`, Punctuation, Push("map_val")},
  55. {`:=`, Punctuation, Push("map_val")},
  56. {`\}`, Punctuation, Pop(1)},
  57. },
  58. "map_val": {
  59. Include("root"),
  60. {`,`, Punctuation, Pop(1)},
  61. {`(?=\})`, Punctuation, Pop(1)},
  62. },
  63. },
  64. ))