This is an intermediary commit, not the final version of dyner.
[dyninst.git] / dyner / src / cmdline.l
1
2 %{
3 #include <string.h>
4 #include "BPatch_snippet.h"
5 #include "dyner.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                     dynerlval.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]+                  { dynerlval.ival = atoi(yytext); return NUMBER; }
90
91 [A-za-z][A-Za-z0-9_]*   { dynerlval.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 PLUSPLUS; }
127
128 "--"                    { return MINUSMINUS; }
129
130 .                       { return ERROR; }
131
132 %%
133
134 void set_lex_input(char *str)
135 {
136     static int firstCall = 1;
137
138     input_str = str;
139
140     if (!firstCall) yy_flush_buffer(YY_CURRENT_BUFFER);
141     firstCall = 0;
142 }
143
144 #ifdef FLEX_SCANNER
145 int my_yyinput(char *buf, int max_size)
146 {
147     int sz = 0;
148
149     while (*input_str != '\0' && sz < max_size) {
150         *buf++ = *input_str++;
151         sz++;
152     }
153
154     return sz;
155 }
156 #endif
157
158 int yywrap()
159 {
160     return 1;
161 }