[0e1846] | 1 | %{ |
---|
| 2 | /**************************************** |
---|
| 3 | * Computer Algebra System SINGULAR * |
---|
| 4 | ****************************************/ |
---|
[ec7aac] | 5 | /* $Id: scanner.l,v 1.28 2000-09-04 13:39:06 obachman Exp $ */ |
---|
[0e1846] | 6 | #include <stdio.h> |
---|
| 7 | #include <string.h> |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | #include <ctype.h> |
---|
| 10 | |
---|
| 11 | #include "mod2.h" |
---|
[c232af] | 12 | #include <omalloc.h> |
---|
[0e1846] | 13 | #include "tok.h" |
---|
| 14 | #include "stype.h" |
---|
| 15 | #include "ipshell.h" |
---|
| 16 | #include "febase.h" |
---|
| 17 | |
---|
[057e93c] | 18 | int feReadLine(char* b, int l); |
---|
[c232af] | 19 | #define ALLOC(a) omAlloc((a)) |
---|
[0e1846] | 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 | |
---|
[ec7aac] | 27 | // this is to shadow the malloc/realloc |
---|
| 28 | // used by yy_flex_malloc/yy_flex_realloc |
---|
| 29 | // so that we can mark stuff as static |
---|
| 30 | static void* my_malloc(size_t size) |
---|
| 31 | { |
---|
| 32 | void* addr = omAlloc(size); |
---|
| 33 | omMarkAsStaticAddr(addr); |
---|
| 34 | return addr; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | static void* my_realloc(void* addr, size_t size) |
---|
| 38 | { |
---|
| 39 | void* new_addr = omRealloc(addr, size); |
---|
| 40 | omMarkAsStaticAddr(new_addr); |
---|
| 41 | return new_addr; |
---|
| 42 | } |
---|
| 43 | static void my_free(void* addr) |
---|
| 44 | { |
---|
| 45 | omFree(addr); |
---|
| 46 | } |
---|
| 47 | #undef malloc |
---|
| 48 | #define malloc my_malloc |
---|
| 49 | #undef realloc |
---|
| 50 | #define realloc my_realloc |
---|
| 51 | #undef free |
---|
| 52 | #define free my_free |
---|
[0e1846] | 53 | static char * dupyytext() |
---|
| 54 | { |
---|
[c232af] | 55 | char* s; |
---|
[0e1846] | 56 | if (yyleng>0) yytext[yyleng-1] = '\0'; |
---|
[c232af] | 57 | s = omStrDup((char *)yytext); |
---|
| 58 | omMarkAsStaticAddr(s); |
---|
| 59 | return s; |
---|
[0e1846] | 60 | } |
---|
| 61 | |
---|
| 62 | static char * dupyytextNL() |
---|
| 63 | { |
---|
| 64 | int i = yyleng;//strlen((char *)yytext); |
---|
[c232af] | 65 | char * rc = (char*)omAlloc( 3 + i ); |
---|
| 66 | omMarkAsStaticAddr(rc); |
---|
[0e1846] | 67 | if (i>0) |
---|
| 68 | { |
---|
[e9ad8a6] | 69 | strncpy( rc, (char *)yytext, i-1 ); |
---|
[0e1846] | 70 | } |
---|
| 71 | else |
---|
[e9ad8a6] | 72 | { |
---|
[0e1846] | 73 | i++; |
---|
[e9ad8a6] | 74 | } |
---|
[0e1846] | 75 | rc[i-1] = '\n'; |
---|
| 76 | rc[i] = '\n'; |
---|
| 77 | rc[i+1] = '\0'; |
---|
| 78 | return rc; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | #undef YY_DECL |
---|
| 82 | #define YY_DECL int yylex(YYSTYPE* lvalp) |
---|
| 83 | |
---|
| 84 | #undef yywrap |
---|
[057e93c] | 85 | extern "C" { |
---|
[0e1846] | 86 | int yywrap() { return exitVoice(); } |
---|
[057e93c] | 87 | } |
---|
[0e1846] | 88 | |
---|
| 89 | #undef YY_INPUT |
---|
| 90 | #define YY_INPUT(buf,result,max_size) \ |
---|
[057e93c] | 91 | result = feReadLine( (char *) (buf), (max_size) ) |
---|
[0e1846] | 92 | |
---|
| 93 | #undef YY_USER_ACTION |
---|
| 94 | #define YY_USER_ACTION \ |
---|
[057e93c] | 95 | if ((inerror==1)&&(*yytext>=' '))\ |
---|
| 96 | { Print(" skipping text from `%s`",yytext);inerror=2; } |
---|
[0e1846] | 97 | |
---|
| 98 | %} |
---|
| 99 | |
---|
| 100 | digit [0-9] |
---|
| 101 | letter [@a-zA-Z\'] |
---|
| 102 | integer {digit}+ |
---|
| 103 | monom {letter}+{digit}* |
---|
| 104 | rgvars ({digit}+[/])*{digit}+{monom}+ |
---|
[b7b08c] | 105 | realnum {digit}*"."{digit}+("e"[+-]{digit}+)? |
---|
[0e1846] | 106 | name ({letter}({letter}*{digit}*_*)*|_) |
---|
| 107 | parname # |
---|
| 108 | |
---|
| 109 | /* %start START */ |
---|
[991705] | 110 | %option always-interactive |
---|
[0e1846] | 111 | |
---|
| 112 | %x string |
---|
| 113 | %x block |
---|
[6ae4f5] | 114 | %x blockstr |
---|
[0e1846] | 115 | %x brace |
---|
| 116 | %x bracestr |
---|
| 117 | %x bracket |
---|
| 118 | %x asstring |
---|
| 119 | |
---|
| 120 | %% |
---|
| 121 | \/\/[^\n]* { } |
---|
| 122 | ^#![^\n]* { } |
---|
[e9ad8a6] | 123 | "/*" { |
---|
| 124 | yy_noeof=noeof_comment; |
---|
| 125 | loop |
---|
| 126 | { |
---|
| 127 | register int c; |
---|
| 128 | while ( (c = yyinput()) != '*' && c != EOF ); |
---|
| 129 | if ( c == '*' ) |
---|
| 130 | { |
---|
| 131 | while ( (c = yyinput()) == '*' ); |
---|
| 132 | if ( c == '/' ) break; /* found the end */ |
---|
| 133 | } |
---|
| 134 | else |
---|
| 135 | { |
---|
| 136 | break; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | yy_noeof=0; |
---|
| 140 | } |
---|
[ff2c077] | 141 | while { prompt_char='.'; |
---|
| 142 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
[0e1846] | 143 | return WHILE_CMD;} |
---|
[ff2c077] | 144 | for { prompt_char='.'; |
---|
| 145 | blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace); |
---|
[0e1846] | 146 | return FOR_CMD;} |
---|
| 147 | |
---|
[cc94b0a] | 148 | ("help"|"?")[ \t\n]* { yy_noeof = noeof_asstring; |
---|
[0e1846] | 149 | BEGIN(asstring); |
---|
| 150 | return HELP_CMD; |
---|
| 151 | } |
---|
| 152 | |
---|
[c04b94] | 153 | example[ \t\n]* { yy_noeof = noeof_asstring; |
---|
| 154 | BEGIN(asstring); |
---|
| 155 | return EXAMPLE_CMD; |
---|
| 156 | } |
---|
| 157 | |
---|
[0e1846] | 158 | proc[ \t]+{name}[ \t]*\( { |
---|
| 159 | char c; char *cp; |
---|
[c232af] | 160 | lvalp->name = omStrDup(iiProcName((char *)yytext,c,cp)); |
---|
[cc94b0a] | 161 | yy_noeof = noeof_procname; |
---|
[0e1846] | 162 | blocknest = 1; |
---|
| 163 | BEGIN(brace); |
---|
| 164 | return PROC_DEF; |
---|
| 165 | } |
---|
| 166 | <asstring>[^;\n]+ { |
---|
[c232af] | 167 | lvalp->name = omStrDup((char *)yytext); |
---|
[cc94b0a] | 168 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 169 | return STRINGTOK; |
---|
| 170 | } |
---|
| 171 | <asstring>; { |
---|
[cc94b0a] | 172 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 173 | return *yytext; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | <brace>"\"" { |
---|
[cc94b0a] | 177 | yy_noeof = noeof_string; |
---|
[0e1846] | 178 | BEGIN(bracestr); |
---|
| 179 | yymore(); |
---|
| 180 | } |
---|
| 181 | <brace>"(" { if (blocknest++) yymore(); } |
---|
| 182 | <brace>[^;\(\)] { if (blocknest) yymore(); } |
---|
| 183 | <brace>";" { |
---|
| 184 | if (blocknest) |
---|
| 185 | { |
---|
| 186 | lvalp->name = dupyytext(); |
---|
| 187 | return STRINGTOK; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | <brace>")" { |
---|
| 191 | if (--blocknest <= 0) |
---|
| 192 | { |
---|
[cc94b0a] | 193 | yy_noeof = 0; |
---|
[0e1846] | 194 | BEGIN(INITIAL); |
---|
| 195 | lvalp->name = dupyytext(); |
---|
| 196 | return STRINGTOK; |
---|
| 197 | } |
---|
| 198 | yymore(); |
---|
| 199 | } |
---|
| 200 | <bracestr>"\"" { |
---|
[cc94b0a] | 201 | yy_noeof = noeof_brace; |
---|
[0e1846] | 202 | BEGIN(brace); |
---|
| 203 | yymore(); |
---|
| 204 | } |
---|
| 205 | <bracestr>[^\"] { yymore(); } |
---|
| 206 | <bracket>"(" { return '('; } |
---|
| 207 | <bracket>"," { return ','; } |
---|
| 208 | <bracket>[ \t\n]* { ; } |
---|
| 209 | <bracket>[^\(\), \t\n]* { |
---|
[c232af] | 210 | lvalp->name = omStrDup((char *)yytext); |
---|
[0e1846] | 211 | return STRINGTOK; |
---|
| 212 | } |
---|
| 213 | <bracket>\"[^\"]*\" { |
---|
[c232af] | 214 | lvalp->name = omStrDup((char *)yytext); |
---|
[0e1846] | 215 | return STRINGTOK; |
---|
| 216 | } |
---|
| 217 | <bracket>")" { |
---|
[cc94b0a] | 218 | yy_noeof = 0; BEGIN(INITIAL); |
---|
[0e1846] | 219 | return ')'; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | "{" { |
---|
[cc94b0a] | 223 | yy_blocklineno = yylineno; |
---|
[0e1846] | 224 | blocknest = 1; |
---|
[cc94b0a] | 225 | yy_noeof = noeof_block; |
---|
[0e1846] | 226 | BEGIN(block); |
---|
| 227 | } |
---|
[6ae4f5] | 228 | <block>"\"" { |
---|
[cc94b0a] | 229 | yy_noeof = noeof_string; |
---|
[6ae4f5] | 230 | BEGIN(blockstr); |
---|
| 231 | yymore(); |
---|
| 232 | } |
---|
| 233 | <blockstr>[^\"] { yymore(); } |
---|
| 234 | <blockstr>"\\\\" { yymore(); } |
---|
| 235 | <blockstr>"\\\"" { yymore(); } |
---|
| 236 | <blockstr>"\"" { |
---|
[cc94b0a] | 237 | yy_noeof = noeof_block; |
---|
[6ae4f5] | 238 | BEGIN(block); |
---|
| 239 | yymore(); |
---|
| 240 | } |
---|
| 241 | <block>[^\{\}\"]* { yymore(); } |
---|
[c656484] | 242 | <block>\/\/[^\n]* { yymore(); } |
---|
[0e1846] | 243 | <block>"{" { blocknest++; yymore(); } |
---|
| 244 | <block>"}" { |
---|
| 245 | if (--blocknest <= 0) |
---|
| 246 | { |
---|
| 247 | BEGIN(INITIAL); |
---|
[cc94b0a] | 248 | yy_noeof = 0; |
---|
[0e1846] | 249 | lvalp->name = dupyytextNL(); |
---|
| 250 | return BLOCKTOK; |
---|
| 251 | } |
---|
| 252 | yymore(); |
---|
| 253 | } |
---|
[cc94b0a] | 254 | "\"" { BEGIN(string); yy_noeof = noeof_string;} |
---|
[0e1846] | 255 | ~ { return SYS_BREAK; } |
---|
| 256 | <string>[^\"] { yymore(); } |
---|
| 257 | <string>"\\\\" { yymore(); } |
---|
| 258 | <string>"\\\"" { yymore(); } |
---|
| 259 | <string>"\"" { |
---|
| 260 | char * s; |
---|
[cc94b0a] | 261 | yy_noeof = 0; |
---|
[0e1846] | 262 | BEGIN(INITIAL); |
---|
| 263 | s = lvalp->name = dupyytext(); |
---|
| 264 | while (*yytext) |
---|
| 265 | { |
---|
| 266 | if (*yytext == '\\') yytext++; |
---|
| 267 | *s++ = *yytext++; |
---|
| 268 | } |
---|
| 269 | *s++ = *yytext++; |
---|
| 270 | return STRINGTOK; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | [ \t\r\n] /* skip whitespace */ |
---|
| 274 | ".." { return DOTDOT; } |
---|
[057e93c] | 275 | "::" { return COLONCOLON; } |
---|
[0e1846] | 276 | "--" { return MINUSMINUS; } |
---|
| 277 | "++" { return PLUSPLUS ; } |
---|
| 278 | "==" { return EQUAL_EQUAL; } |
---|
[7b3094] | 279 | "&&" { lvalp->i='&'; return LOGIC_OP; } |
---|
| 280 | "||" { lvalp->i='|'; return LOGIC_OP; } |
---|
| 281 | "<=" { lvalp->i=LE; return COMP_OP; } |
---|
| 282 | ">=" { lvalp->i=GE; return COMP_OP; } |
---|
[0e1846] | 283 | "!" { return NOT; } |
---|
| 284 | "!=" { return NOTEQUAL; } |
---|
| 285 | "<>" { return NOTEQUAL; } |
---|
| 286 | "**" { return '^'; } |
---|
| 287 | \\ { return '\\'; } |
---|
| 288 | newline { |
---|
[c232af] | 289 | lvalp->name = omStrDup("\n"); |
---|
[0e1846] | 290 | return STRINGTOK; |
---|
| 291 | } |
---|
| 292 | {integer} { |
---|
| 293 | lvalp->name = (char *)yytext; |
---|
[86050e] | 294 | if ((currRing!=NULL) |
---|
| 295 | && (strlen(lvalp->name)>MAX_INT_LEN)) |
---|
| 296 | return RINGVAR; |
---|
| 297 | else |
---|
| 298 | return INT_CONST; |
---|
[0e1846] | 299 | } |
---|
| 300 | {integer}\/{integer} { |
---|
[6bb498] | 301 | lvalp->name = (char *)yytext; |
---|
[0e1846] | 302 | return RINGVAR; |
---|
| 303 | } |
---|
[b7b08c] | 304 | \$ { |
---|
[e9ad8a6] | 305 | m2_end(-1); |
---|
[0e1846] | 306 | } |
---|
[22d790] | 307 | (quit|exit)[ \t\n]*[\.;]? { |
---|
[0e1846] | 308 | #ifdef MM_STAT |
---|
| 309 | mmStat(-500); |
---|
| 310 | #endif |
---|
[c232af] | 311 | #ifdef OM_TRACK |
---|
| 312 | omPrintUsedTrackAddrs(stdout); |
---|
[0e1846] | 313 | #endif |
---|
[e9ad8a6] | 314 | m2_end(0); |
---|
[0e1846] | 315 | } |
---|
| 316 | |
---|
| 317 | {rgvars}|{realnum} { |
---|
[6bb498] | 318 | lvalp->name = (char *)yytext; |
---|
[0e1846] | 319 | return RINGVAR; |
---|
| 320 | } |
---|
[7b3094] | 321 | [0-9]+\."e"[+-][0-9]+ { |
---|
[b7b08c] | 322 | lvalp->name = (char *)yytext; |
---|
| 323 | return RINGVAR; |
---|
| 324 | } |
---|
| 325 | [0-9]+\./[^\.] { |
---|
| 326 | lvalp->name = (char *)yytext; |
---|
| 327 | return RINGVAR; |
---|
| 328 | } |
---|
[7b3094] | 329 | |
---|
[0e1846] | 330 | ({parname}|{name}) { |
---|
| 331 | /* {name} */ |
---|
| 332 | int rc=0; |
---|
| 333 | if (yytext[strlen((char *)yytext)-1] == '\n') |
---|
| 334 | { |
---|
| 335 | yytext[strlen((char *)yytext)-1] = '\0'; |
---|
| 336 | } |
---|
| 337 | if (yyleng > 1) |
---|
| 338 | { |
---|
| 339 | rc = IsCmd((char *)yytext,lvalp->i); |
---|
| 340 | if (rc) return rc; |
---|
| 341 | } |
---|
[c232af] | 342 | lvalp->name = omStrDup((char *)yytext); |
---|
[0e1846] | 343 | return UNKNOWN_IDENT; |
---|
| 344 | } |
---|
| 345 | |
---|
[057e93c] | 346 | . { |
---|
[0e1846] | 347 | /*if (*yytext == '\n') REJECT;*/ |
---|
[7b3094] | 348 | register char ch= *yytext; |
---|
| 349 | lvalp->i = ch; |
---|
| 350 | switch(ch) |
---|
| 351 | { |
---|
| 352 | /* case '&': */ |
---|
| 353 | case '|': |
---|
| 354 | return LOGIC_OP; |
---|
| 355 | /* case '/': */ |
---|
| 356 | case '%': |
---|
| 357 | case '*': |
---|
| 358 | return MULDIV_OP; |
---|
| 359 | /* case '<': */ |
---|
| 360 | case '>': |
---|
| 361 | return COMP_OP; |
---|
| 362 | default: |
---|
| 363 | break; |
---|
| 364 | } |
---|
| 365 | return ch; |
---|
[0e1846] | 366 | } |
---|
| 367 | %% |
---|
| 368 | |
---|
| 369 | void * myynewbuffer() |
---|
| 370 | { |
---|
| 371 | void * oldb = yy_current_buffer; |
---|
[057e93c] | 372 | yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE)); |
---|
[0e1846] | 373 | return oldb; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | void myyoldbuffer(void * oldb) |
---|
| 377 | { |
---|
| 378 | yy_delete_buffer(yy_current_buffer); |
---|
| 379 | yy_switch_to_buffer((YY_BUFFER_STATE)oldb); |
---|
[057e93c] | 380 | //yy_flush_buffer((YY_BUFFER_STATE)oldb); |
---|
[0e1846] | 381 | } |
---|