-
Notifications
You must be signed in to change notification settings - Fork 4
/
docopt.l
70 lines (56 loc) · 1.33 KB
/
docopt.l
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
%{
#include "docopt.tab.h"
static inline void yylval_setstr(const char *ptr, size_t len)
{
yylval.str.len = len;
yylval.str.ptr = ptr;
}
%}
%option nounput
%option noinput
%option nodefault
%option yylineno
%s OPTIONS USAGE
NUM ([0-9]*)
ARG ([a-zA-Z_][a-zA-Z0-9\-_.]*)
POSARG (\<{ARG}\>)
POSARG_DDD ({POSARG}[.]{3})
WORD ([a-zA-Z0-9\-_.<>]*)
ANY ([^ \t\n]*)
%%
/* single character ops */
"=" |
"|" |
"(" |
")" |
"[" |
"]" { return yytext[0]; }
Options([ ]*): { BEGIN(OPTIONS); }
Usage([ ]*): { BEGIN(USAGE); }
<INITIAL>{ANY}
<OPTIONS>{ANY} /* TODO: currently ignore options */
<USAGE>{NUM} { yylval_setstr(yytext, yyleng); return WORD; }
<USAGE>--{ARG} { yylval_setstr(yytext, yyleng); return OPTARG; }
<USAGE>-{ARG} { yylval_setstr(yytext, yyleng); return OPTARG; }
<USAGE>{POSARG_DDD} { yylval_setstr(yytext + 1, yyleng - 5); return POSARG_DDD; }
<USAGE>{POSARG} { yylval_setstr(yytext + 1, yyleng - 2); return POSARG; }
<USAGE>{ARG} { yylval_setstr(yytext, yyleng); return ARG; }
<USAGE>{WORD} { yylval_setstr(yytext, yyleng); return WORD; }
[ \t]+ /* ignore whitespaces */
"\n" { return EOL; }
. { return yytext[0]; }
%%
void lex_beginUSAGE(void)
{
BEGIN(USAGE);
}
int yywrap(void)
{
return 1;
}
#if 0
main(int argc, char **argv)
{
yylex();
}
#endif