File: tokenizer.l

package info (click to toggle)
ruby-github-linguist 7.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,204 kB
  • sloc: ruby: 1,872; lex: 173; ansic: 35; makefile: 9
file content (173 lines) | stat: -rw-r--r-- 6,695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
%{

#include "ruby.h"

// Anything longer is unlikely to be useful.
#define MAX_TOKEN_LEN 16

#define FEED2(s, l) do { \
    const char* __s = (s); \
    const size_t __l = (l); \
    const size_t __cl = __l > MAX_TOKEN_LEN? MAX_TOKEN_LEN : __l; \
    *yyextra = rb_str_new(__s, __cl); \
  } while(0)

#define FEED1(s) FEED2(s, strlen(s))

#define FEED() FEED2(yytext, yyleng)

#define FEED_STATIC(s) FEED2(s, sizeof(s) - 1)

#define FEED_SHEBANG(s) do { \
    const size_t __l = strlen(s); \
    const size_t __cl = __l > MAX_TOKEN_LEN? MAX_TOKEN_LEN : __l; \
    *yyextra = rb_str_new("SHEBANG#!", sizeof("SHEBANG#!") - 1); \
    rb_str_cat(*yyextra, s, __cl); \
  } while(0)

#define eat_until_eol() do { \
    int c; \
    while ((c = input(yyscanner)) != '\n' && c != EOF && c); \
    if (c == EOF || !c) \
      return 0; \
  } while (0)

#define eat_until_unescaped(q) do { \
    int c; \
    while ((c = input(yyscanner)) != EOF && c) { \
      if (c == '\n') \
        break; \
      if (c == '\\') { \
        c = input(yyscanner); \
        if (c == EOF || !c) \
          return 0; \
      } else if (c == q) \
        break; \
    } \
    if (c == EOF || !c) \
      return 0; \
  } while (0)

%}

%option never-interactive yywrap reentrant nounput warn nodefault header-file="lex.linguist_yy.h" extra-type="VALUE*" prefix="linguist_yy"
%x c_comment xml_comment haskell_comment ocaml_comment python_dcomment python_scomment roff_comment

%%

^#![ \t]*([[:alnum:]_\/]*\/)?env([ \t]+([^ \t=]*=[^ \t]*))*[ \t]+[[:alpha:]_]+ {
  const char *off = strrchr(yytext, ' ');
  if (!off)
    off = yytext;
  else
    ++off;
  FEED_SHEBANG(off);
  eat_until_eol();
  return 1;
}

^#![ \t]*[[:alpha:]_\/]+  {
  const char *off = strrchr(yytext, '/');
  if (!off)
    off = yytext;
  else
   ++off;
  if (strcmp(off, "env") == 0) {
    eat_until_eol();
  } else {
    FEED_SHEBANG(off);
    eat_until_eol();
    return 1;
  }
}

^[ \t]*[#]+(" ".*|\n)    { FEED_STATIC("COMMENT#"); return 1; }
^[ \t]*"//!"(" ".*|\n)   { FEED_STATIC("COMMENT//!"); return 1; }
^[ \t]*"//".*[\n]?       { FEED_STATIC("COMMENT//"); return 1; }
^[ \t]*"--"(" ".*|\n)    { FEED_STATIC("COMMENT--"); return 1; }
^[ \t]*[%]+(" ".*|\n)    { FEED_STATIC("COMMENT%"); return 1; }
^[ \t]*\"(" ".*|\n)      { FEED_STATIC("COMMENT\""); return 1; }
^[ \t]*;+(" ".*|\n)      { FEED_STATIC("COMMENT;"); return 1; }
^[.][ \t]*\\\"(.*|\n)    { FEED_STATIC("COMMENT.\\\""); return 1; }
^['][ \t]*\\\"(.*|\n)    { FEED_STATIC("COMMENT'\\\""); return 1; }
^"$! "(.*|\n)            { FEED_STATIC("COMMENT$!"); return 1; }

"/**/"                   { FEED_STATIC("COMMENT/*"); return 1; }
"/**"                    { FEED_STATIC("COMMENT/**"); BEGIN(c_comment); return 1; }
"/*!"                    { FEED_STATIC("COMMENT/*!"); BEGIN(c_comment); return 1; }
"/*"                     { FEED_STATIC("COMMENT/*"); BEGIN(c_comment); return 1; }
"<!--"                   { FEED_STATIC("COMMENT<!--"); BEGIN(xml_comment); return 1; }
"{-"                     { FEED_STATIC("COMMENT{-"); BEGIN(haskell_comment); return 1; }
"(*"                     { FEED_STATIC("COMMENT(*"); BEGIN(ocaml_comment); return 1; }
"\"\"\""                 { FEED_STATIC("COMMENT\"\"\""); BEGIN(python_dcomment); return 1; }
"'''"                    { FEED_STATIC("COMMENT'''"); BEGIN(python_scomment); return 1; }
^".ig"\n                 { FEED_STATIC("COMMENT.ig"); BEGIN(roff_comment); return 1; }

<c_comment,xml_comment,haskell_comment,ocaml_comment,python_dcomment,python_scomment,roff_comment>.|\n { /* nothing */ }
<c_comment>"*/"                   { BEGIN(INITIAL); }
<xml_comment>"-->"                { BEGIN(INITIAL); }
<haskell_comment>"-}"             { BEGIN(INITIAL); }
<ocaml_comment>"*)"               { BEGIN(INITIAL); }
<python_dcomment>"\"\"\""         { BEGIN(INITIAL); }
<python_scomment>"'''"            { BEGIN(INITIAL); }
<roff_comment>".."\n              { BEGIN(INITIAL); }

\"\"|''                           { /* nothing */ }
\"                                { eat_until_unescaped('"'); }
'                                 { eat_until_unescaped('\''); }
(0x[0-9a-fA-F]([0-9a-fA-F]|\.)*|[0-9]([0-9]|\.)*)([uU][lL]{0,2}|([eE][-+][0-9]*)?[fFlL]*) { /* nothing */ }

[.@#$]?[[:alnum:]_]+              { FEED(); return 1; }

[(]+[)]+                          { FEED(); return 1; }
[{]+[}]+                          { FEED(); return 1; }
[\[]+[\]]+                        { FEED(); return 1; }
[(]+|[)]+                         { FEED(); return 1; }
[{]+|[}]+                         { FEED(); return 1; }
[\[]+|[\]]+                       { FEED(); return 1; }
[$]([(]+|[{]+|[\[]]+)             { FEED(); return 1; }

"(...)"|"{...}"|"[...]"           { FEED(); return 1; }

"&>"|"<&"|"<&-"|"&>>"|">&"        { FEED(); return 1; }
"|&"|"&|"                         { FEED(); return 1; }

[-]+[>]+                          { FEED(); return 1; }
[<]+[-]+                          { FEED(); return 1; }

[!]+[=]+                          { FEED(); return 1; }
[<>]*[=]+[<>]*                    { FEED(); return 1; }
[<][/]?[?%!#@]                    { FEED(); return 1; }
[?%!][>]                          { FEED(); return 1; }
[<>/]+                            { FEED(); return 1; }
[-+*/%&|^~:][=]+                  { FEED(); return 1; }
[!=][~]                           { FEED(); return 1; }
":-"                              { FEED(); return 1; }

[.][*]+[?]?                       { FEED(); return 1; }
[.][+]+[?]?                       { FEED(); return 1; }
"(?:"                             { FEED(); return 1; }

[-]+                              { FEED(); return 1; }
[!]+                              { FEED(); return 1; }
[#]+                              { FEED(); return 1; }
[$]+                              { FEED(); return 1; }
[%]+                              { FEED(); return 1; }
[&]+                              { FEED(); return 1; }
[*]+                              { FEED(); return 1; }
[+]+                              { FEED(); return 1; }
[,]+                              { FEED(); return 1; }
[.]+                              { FEED(); return 1; }
[:]+                              { FEED(); return 1; }
[;]+                              { FEED(); return 1; }
[?]+                              { FEED(); return 1; }
[@]+                              { FEED(); return 1; }
[\\]+                             { FEED(); return 1; }
[\^]+                             { FEED(); return 1; }
[`]+                              { FEED(); return 1; }
[|]+                              { FEED(); return 1; }
[~]+                              { FEED(); return 1; }

.|\n                              { /* nothing */ }

%%