[0e1846] | 1 | %{ |
---|
| 2 | /**************************************** |
---|
| 3 | * Computer Algebra System SINGULAR * |
---|
| 4 | ****************************************/ |
---|
[86050e] | 5 | /* $Id: scanner.l,v 1.20 1999-07-17 14:01:46 Singular Exp $ */ |
---|
[0e1846] | 6 | #include <stdio.h> |
---|
| 7 | #include <string.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | #include <ctype.h> |
---|
| 10 | |
---|
| 11 | #include "mod2.h" |
---|
| 12 | #include "tok.h" |
---|
| 13 | #include "stype.h" |
---|
| 14 | #include "ipshell.h" |
---|
| 15 | #include "mmemory.h" |
---|
| 16 | #include "febase.h" |
---|
| 17 | |
---|
[057e93c] | 18 | int feReadLine(char* b, int l); |
---|
[0e1846] | 19 | #define ALLOC(a) Alloc((a)) |
---|
| 20 | int yylineno = 0; |
---|
[64d729] | 21 | int blocknest = 0; |
---|
[0e1846] | 22 | extern char * yytext; |
---|
| 23 | //extern unsigned char * yytext; |
---|
| 24 | extern int yyleng; |
---|
[530bdb] | 25 | extern int inerror; |
---|
[0e1846] | 26 | |
---|
| 27 | static char * dupyytext() |
---|
| 28 | { |
---|
| 29 | if (yyleng>0) yytext[yyleng-1] = '\0'; |
---|
| 30 | return mstrdup((char *)yytext); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | static char * dupyytextNL() |
---|
| 34 | { |
---|
| 35 | int i = yyleng;//strlen((char *)yytext); |
---|
| 36 | char * rc = (char*)AllocL( 3 + i ); |
---|
| 37 | if (i>0) |
---|
| 38 | { |
---|
[e9ad8a6] | 39 | strncpy( rc, (char *)yytext, i-1 ); |
---|
[0e1846] | 40 | } |
---|
| 41 | else |
---|
[e9ad8a6] | 42 | { |
---|
[0e1846] | 43 | i++; |
---|
[e9ad8a6] | 44 | } |
---|
[0e1846] | 45 | rc[i-1] = '\n'; |
---|
| 46 | rc[i] = '\n'; |
---|
| 47 | rc[i+1] = '\0'; |
---|
| 48 | return rc; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | #undef YY_DECL |
---|
| 52 | #define YY_DECL int yylex(YYSTYPE* lvalp) |
---|
| 53 | |
---|
| 54 | #undef yywrap |
---|
[057e93c] | 55 | extern "C" { |
---|
[0e1846] | 56 | int yywrap() { return exitVoice(); } |
---|
[057e93c] | 57 | } |
---|
[0e1846] | 58 | |
---|
| 59 | #undef YY_INPUT |
---|
| 60 | #define YY_INPUT(buf,result,max_size) \ |
---|
[057e93c] | 61 | result = feReadLine( (char *) (buf), (max_size) ) |
---|
[0e1846] | 62 | |
---|
| 63 | #undef YY_USER_ACTION |
---|
| 64 | #define YY_USER_ACTION \ |
---|
[057e93c] | 65 | if ((inerror==1)&&(*yytext>=' '))\ |
---|
| 66 | { Print(" skipping text from `%s`",yytext);inerror=2; } |
---|
[0e1846] | 67 | |
---|
| 68 | %} |
---|
| 69 | |
---|
| 70 | digit [0-9] |
---|
| 71 | letter [@a-zA-Z\'] |
---|
| 72 | integer {digit}+ |
---|
| 73 | monom {letter}+{digit}* |
---|
| 74 | rgvars ({digit}+[/])*{digit}+{monom}+ |
---|
| 75 | realnum {digit}+"."{digit}+("e"[+-]{digit}+)? |
---|
| 76 | name ({letter}({letter}*{digit}*_*)*|_) |
---|
| 77 | parname # |
---|
| 78 | |
---|
| 79 | /* %start START */ |
---|
[991705] | 80 | %option always-interactive |
---|
[0e1846] | 81 | |
---|
| 82 | %x string |
---|
| 83 | %x block |
---|
[6ae4f5] | 84 | %x blockstr |
---|
[0e1846] | 85 | %x brace |
---|
| 86 | %x bracestr |
---|
| 87 | %x bracket |
---|
| 88 | %x asstring |
---|
| 89 | |
---|
| 90 | %% |
---|
| 91 | \/\/[^\n]* { } |
---|
| 92 | ^#![^\n]* { } |
---|
[e9ad8a6] | 93 | "/*" { |
---|
| 94 | yy_noeof=noeof_comment; |
---|
| 95 | loop |
---|
| 96 | { |
---|
| 97 | register int c; |
---|
| 98 | while ( (c = yyinput()) != '*' && c != EOF ); |
---|
| 99 | if ( c == '*' ) |
---|
| 100 | { |
---|
| 101 | while ( (c = yyinput()) == '*' ); |
---|
| 102 | if ( c == '/' ) break; /* found the end */ |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | break; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | yy_noeof=0; |
---|
| 110 | } |
---|
[ff2c077] | 111 | while { prompt_char='.'; |
---|
| 112 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
[0e1846] | 113 | return WHILE_CMD;} |
---|
[ff2c077] | 114 | for { prompt_char='.'; |
---|
| 115 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
[0e1846] | 116 | return FOR_CMD;} |
---|
| 117 | |
---|
[cc94b0a] | 118 | ("help"|"?")[ \t\n]* { yy_noeof = noeof_asstring; |
---|
[0e1846] | 119 | BEGIN(asstring); |
---|
| 120 | return HELP_CMD; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | proc[ \t]+{name}[ \t]*\( { |
---|
| 124 | char c; char *cp; |
---|
| 125 | lvalp->name = mstrdup(iiProcName((char *)yytext,c,cp)); |
---|
[cc94b0a] | 126 | yy_noeof = noeof_procname; |
---|
[0e1846] | 127 | blocknest = 1; |
---|
| 128 | BEGIN(brace); |
---|
| 129 | return PROC_DEF; |
---|
| 130 | } |
---|
| 131 | <asstring>[^;\n]+ { |
---|
| 132 | lvalp->name = mstrdup((char *)yytext); |
---|
[cc94b0a] | 133 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 134 | return STRINGTOK; |
---|
| 135 | } |
---|
| 136 | <asstring>; { |
---|
[cc94b0a] | 137 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 138 | return *yytext; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | <brace>"\"" { |
---|
[cc94b0a] | 142 | yy_noeof = noeof_string; |
---|
[0e1846] | 143 | BEGIN(bracestr); |
---|
| 144 | yymore(); |
---|
| 145 | } |
---|
| 146 | <brace>"(" { if (blocknest++) yymore(); } |
---|
| 147 | <brace>[^;\(\)] { if (blocknest) yymore(); } |
---|
| 148 | <brace>";" { |
---|
| 149 | if (blocknest) |
---|
| 150 | { |
---|
| 151 | lvalp->name = dupyytext(); |
---|
| 152 | return STRINGTOK; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | <brace>")" { |
---|
| 156 | if (--blocknest <= 0) |
---|
| 157 | { |
---|
[cc94b0a] | 158 | yy_noeof = 0; |
---|
[0e1846] | 159 | BEGIN(INITIAL); |
---|
| 160 | lvalp->name = dupyytext(); |
---|
| 161 | return STRINGTOK; |
---|
| 162 | } |
---|
| 163 | yymore(); |
---|
| 164 | } |
---|
| 165 | <bracestr>"\"" { |
---|
[cc94b0a] | 166 | yy_noeof = noeof_brace; |
---|
[0e1846] | 167 | BEGIN(brace); |
---|
| 168 | yymore(); |
---|
| 169 | } |
---|
| 170 | <bracestr>[^\"] { yymore(); } |
---|
| 171 | <bracket>"(" { return '('; } |
---|
| 172 | <bracket>"," { return ','; } |
---|
| 173 | <bracket>[ \t\n]* { ; } |
---|
| 174 | <bracket>[^\(\), \t\n]* { |
---|
| 175 | lvalp->name = mstrdup((char *)yytext); |
---|
| 176 | return STRINGTOK; |
---|
| 177 | } |
---|
| 178 | <bracket>\"[^\"]*\" { |
---|
| 179 | lvalp->name = mstrdup((char *)yytext); |
---|
| 180 | return STRINGTOK; |
---|
| 181 | } |
---|
| 182 | <bracket>")" { |
---|
[cc94b0a] | 183 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 184 | return ')'; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | "{" { |
---|
[cc94b0a] | 188 | yy_blocklineno = yylineno; |
---|
[0e1846] | 189 | blocknest = 1; |
---|
[cc94b0a] | 190 | yy_noeof = noeof_block; |
---|
[0e1846] | 191 | BEGIN(block); |
---|
| 192 | } |
---|
[6ae4f5] | 193 | <block>"\"" { |
---|
[cc94b0a] | 194 | yy_noeof = noeof_string; |
---|
[6ae4f5] | 195 | BEGIN(blockstr); |
---|
| 196 | yymore(); |
---|
| 197 | } |
---|
| 198 | <blockstr>[^\"] { yymore(); } |
---|
| 199 | <blockstr>"\\\\" { yymore(); } |
---|
| 200 | <blockstr>"\\\"" { yymore(); } |
---|
| 201 | <blockstr>"\"" { |
---|
[cc94b0a] | 202 | yy_noeof = noeof_block; |
---|
[6ae4f5] | 203 | BEGIN(block); |
---|
| 204 | yymore(); |
---|
| 205 | } |
---|
| 206 | <block>[^\{\}\"]* { yymore(); } |
---|
[c656484] | 207 | <block>\/\/[^\n]* { yymore(); } |
---|
[0e1846] | 208 | <block>"{" { blocknest++; yymore(); } |
---|
| 209 | <block>"}" { |
---|
| 210 | if (--blocknest <= 0) |
---|
| 211 | { |
---|
| 212 | BEGIN(INITIAL); |
---|
[cc94b0a] | 213 | yy_noeof = 0; |
---|
[0e1846] | 214 | lvalp->name = dupyytextNL(); |
---|
| 215 | return BLOCKTOK; |
---|
| 216 | } |
---|
| 217 | yymore(); |
---|
| 218 | } |
---|
[cc94b0a] | 219 | "\"" { BEGIN(string); yy_noeof = noeof_string;} |
---|
[0e1846] | 220 | ~ { return SYS_BREAK; } |
---|
| 221 | <string>[^\"] { yymore(); } |
---|
| 222 | <string>"\\\\" { yymore(); } |
---|
| 223 | <string>"\\\"" { yymore(); } |
---|
| 224 | <string>"\"" { |
---|
| 225 | char * s; |
---|
[cc94b0a] | 226 | yy_noeof = 0; |
---|
[0e1846] | 227 | BEGIN(INITIAL); |
---|
| 228 | s = lvalp->name = dupyytext(); |
---|
| 229 | while (*yytext) |
---|
| 230 | { |
---|
| 231 | if (*yytext == '\\') yytext++; |
---|
| 232 | *s++ = *yytext++; |
---|
| 233 | } |
---|
| 234 | *s++ = *yytext++; |
---|
| 235 | return STRINGTOK; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | [ \t\r\n] /* skip whitespace */ |
---|
| 239 | ".." { return DOTDOT; } |
---|
[057e93c] | 240 | "::" { return COLONCOLON; } |
---|
[0e1846] | 241 | "--" { return MINUSMINUS; } |
---|
| 242 | "++" { return PLUSPLUS ; } |
---|
| 243 | "==" { return EQUAL_EQUAL; } |
---|
| 244 | "&&" { return '&'; } |
---|
| 245 | "||" { return '|'; } |
---|
| 246 | "<=" { return LE; } |
---|
| 247 | ">=" { return GE; } |
---|
| 248 | "!" { return NOT; } |
---|
| 249 | "!=" { return NOTEQUAL; } |
---|
| 250 | "<>" { return NOTEQUAL; } |
---|
| 251 | "**" { return '^'; } |
---|
| 252 | \\ { return '\\'; } |
---|
| 253 | newline { |
---|
| 254 | lvalp->name = mstrdup("\n"); |
---|
| 255 | return STRINGTOK; |
---|
| 256 | } |
---|
| 257 | {integer} { |
---|
| 258 | lvalp->name = (char *)yytext; |
---|
[86050e] | 259 | if ((currRing!=NULL) |
---|
| 260 | && (strlen(lvalp->name)>MAX_INT_LEN)) |
---|
| 261 | { |
---|
| 262 | lvalp->name = mstrdup((char *)yytext); |
---|
| 263 | return RINGVAR; |
---|
| 264 | } |
---|
| 265 | else |
---|
| 266 | return INT_CONST; |
---|
[0e1846] | 267 | } |
---|
| 268 | {integer}\/{integer} { |
---|
| 269 | lvalp->name = mstrdup((char *)yytext); |
---|
| 270 | return RINGVAR; |
---|
| 271 | } |
---|
[057e93c] | 272 | \$ { |
---|
[e9ad8a6] | 273 | m2_end(-1); |
---|
[0e1846] | 274 | } |
---|
[22d790] | 275 | (quit|exit)[ \t\n]*[\.;]? { |
---|
[0e1846] | 276 | #ifdef MM_STAT |
---|
| 277 | mmStat(-500); |
---|
| 278 | #endif |
---|
[057e93c] | 279 | #ifdef MDEBUG |
---|
[0e1846] | 280 | #ifdef MLIST |
---|
[6e5833] | 281 | mmTestList(0); |
---|
[0e1846] | 282 | #endif |
---|
| 283 | #endif |
---|
[e9ad8a6] | 284 | m2_end(0); |
---|
[0e1846] | 285 | } |
---|
| 286 | |
---|
| 287 | {rgvars}|{realnum} { |
---|
| 288 | lvalp->name = mstrdup((char *)yytext); |
---|
| 289 | return RINGVAR; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | ({parname}|{name}) { |
---|
| 293 | /* {name} */ |
---|
| 294 | int rc=0; |
---|
| 295 | if (yytext[strlen((char *)yytext)-1] == '\n') |
---|
| 296 | { |
---|
| 297 | yytext[strlen((char *)yytext)-1] = '\0'; |
---|
| 298 | } |
---|
| 299 | if (yyleng > 1) |
---|
| 300 | { |
---|
| 301 | rc = IsCmd((char *)yytext,lvalp->i); |
---|
| 302 | if (rc) return rc; |
---|
| 303 | } |
---|
| 304 | lvalp->name = mstrdup((char *)yytext); |
---|
| 305 | return UNKNOWN_IDENT; |
---|
| 306 | } |
---|
| 307 | |
---|
[057e93c] | 308 | . { |
---|
[0e1846] | 309 | /*if (*yytext == '\n') REJECT;*/ |
---|
| 310 | lvalp->i = *yytext; /* token has own value */ |
---|
| 311 | return *yytext; |
---|
| 312 | } |
---|
| 313 | %% |
---|
| 314 | |
---|
| 315 | void * myynewbuffer() |
---|
| 316 | { |
---|
| 317 | void * oldb = yy_current_buffer; |
---|
[057e93c] | 318 | yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE)); |
---|
[0e1846] | 319 | return oldb; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | void myyoldbuffer(void * oldb) |
---|
| 323 | { |
---|
| 324 | yy_delete_buffer(yy_current_buffer); |
---|
| 325 | yy_switch_to_buffer((YY_BUFFER_STATE)oldb); |
---|
[057e93c] | 326 | //yy_flush_buffer((YY_BUFFER_STATE)oldb); |
---|
[0e1846] | 327 | } |
---|