First Commit of this utility to let altinel start working on it.
[dyninst.git] / dyner / src / cmdline.l
1
2 %{
3 #include <string.h>
4 #include "BPatch_snippet.h"
5 #include "y.tab.h"
6
7 extern "C" {
8 void set_lex_input(char *);
9 }
10
11 static char *input_str = NULL;
12
13 #ifdef FLEX_SCANNER
14 #undef YY_INPUT
15
16 #define YY_INPUT(b,r,ms) (r = my_yyinput(b, ms))
17
18 int my_yyinput(char *buf, int max_size);
19 #else
20 #undef input
21 #undef unput
22
23 #define input()  (*input_str++)
24 #define unput(c) (*--input_str = c)
25
26 #endif
27
28
29 static int yyinput(void);
30
31 static int doubleQuote()
32 {
33     int count;
34     char ch;
35     bool backSlash = false;
36
37     count = 0;
38     while (1) {
39         ch = yyinput();
40         yyleng++;
41         switch (ch) {
42             case '"':
43                 if (backSlash) {
44                     /* \" */
45                     yytext[count++] = '"';
46                 } else {
47                     yyleng--;
48                     yytext[count] = '\0';
49                     yylval.sval = strdup(yytext);
50                     return(STRING);
51                 }
52                 break;
53
54             case '\n':
55                 /* New line before end of string */
56                 return(ERROR);
57                 break;
58
59             case '\\':
60                 yytext[count++] = ch;
61                 backSlash = true;
62                 break;
63
64             case 'n':
65                 if (backSlash) {
66                     yytext[count-1] = '\n';
67                 } else {
68                     yytext[count++] = ch;
69                 }
70                 backSlash = false;
71                 break;
72
73             default:
74                 yytext[count++] = ch;
75                 backSlash = false;
76                 break;
77         }
78     }
79 }
80
81 %}
82
83 %%
84
85 if                      { return IF; }
86
87 else                    { return ELSE; }
88
89 [0-9]+                  { yylval.ival = atoi(yytext); return NUMBER; }
90
91 [A-za-z][A-Za-z0-9_]*   { yylval.sval = strdup(yytext); return IDENTIFIER; }
92
93 \<=                     { return LESS_EQ; }
94
95 \>=                     { return GREATER_EQ; }
96
97 ==                      { return EQ; }
98
99 ,                       { return COMMA; }
100
101 =                       { return ASSIGN; }
102
103 !=                      { return NOT_EQ; }
104  
105 \{                      { return START_BLOCK; }
106
107 \}                      { return END_BLOCK; }
108
109 \.                      { return DOT; }
110
111 ;                       { return SEMI; }
112
113 [()<>+*/-]              { return yytext[0]; }
114
115 &&                      { return AND; }
116
117 \|\|                    { return OR; }
118
119 [ \t\n]+                { /* Nothing */; }
120
121 \"                      { int ret;
122                           ret = doubleQuote();
123                           return(ret);
124                         }
125
126 .                       { return ERROR; }
127
128 %%
129
130 void set_lex_input(char *str)
131 {
132     static int firstCall = 1;
133
134     input_str = str;
135
136     if (!firstCall) yy_flush_buffer(YY_CURRENT_BUFFER);
137     firstCall = 0;
138 }
139
140 #ifdef FLEX_SCANNER
141 int my_yyinput(char *buf, int max_size)
142 {
143     int sz = 0;
144
145     while (*input_str != '\0' && sz < max_size) {
146         *buf++ = *input_str++;
147         sz++;
148     }
149
150     return sz;
151 }
152 #endif
153
154 int yywrap()
155 {
156     return 1;
157 }