- int c;
- char line[128];
- char *p;
-
- c = GETC();
- while (1) {
- /* Handle comments first */
- if (c == '/') {
- int c2 = GETC();
- if (c2 == '*' || c2 == '/') {
- discard_comments(c2);
- c = GETC();
- continue;
- }
- else
- UNGETC(c2);
- }
- switch (c) {
- case EOF:
- return 0;
- case ' ':
- case '\t':
- case '\r':
- case '\n':
- /* Skip whitespace */
- break;
- case 'a': case 'b': case 'c': case 'd':
- case 'e': case 'f': case 'g': case 'h':
- case 'i': case 'j': case 'k': case 'l':
- case 'm': case 'n': case 'o': case 'p':
- case 'q': case 'r': case 's': case 't':
- case 'u': case 'v': case 'w': case 'x':
- case 'y': case 'z':
- case 'A': case 'B': case 'C': case 'D':
- case 'E': case 'F': case 'G': case 'H':
- case 'I': case 'J': case 'K': case 'L':
- case 'M': case 'N': case 'O': case 'P':
- case 'Q': case 'R': case 'S': case 'T':
- case 'U': case 'V': case 'W': case 'X':
- case 'Y': case 'Z':
- case '_':
- /* Start of a token. Parse. */
- p = line;
- *p++ = c;
- c = GETC();
- while (ISL(c)) {
- *p++ = c;
- c = GETC();
- }
- *p = '\0';
- UNGETC(c);
- return check_token(line);
- case '0': case '1':
- case '2': case '3': case '4': case '5':
- case '6': case '7': case '8': case '9':
- p = line;
- *p++ = c;
- c = GETC();
- if (c == 'x' || c == 'X') {
- *p++ = c;
- c = GETC();
- }
- while (ISHEX(c)) {
- *p++ = c;
- c = GETC();
- }
- *p = '\0';
- UNGETC(c);
- yylval.val = constVal(line);
- return CONSTANT;
- case '\"':
- /* A string */
- p = stringLiteral();
- yylval.val = strVal(p);
- return(STRING_LITERAL);
- case '\'':
- /* ie '\n' */
- break;
- case '#':
- /* Assume a pragma and toast the rest of the line. */
- c = GETC();
- while (c != '\n') {
- c = GETC();
- }
- break;
- case '=':
- case '&':
- case '!':
- case '-':
- case '+':
- case '*':
- case '/':
- case '%':
- case '<':
- case '>':
- case '^':
- case '|':
- /* Cases which can be compounds */
- return c;
- case '{':
- NestLevel++;
- return c;
- case '}':
- NestLevel--;
- return c;
- case '.':
- c = GETC();
- if (c == '.') {
- c = GETC();
- if (c == '.') {
- return VAR_ARGS;
- }
- }
- UNGETC(c);
- /* Fall through */
- case ',':
- case ':':
- case '(': case ')':
- case '[': case ']':
- case '~':
- case '?':
- /* Special characters that cant be part of a composite */
- return c;
- default:
- printf("Unhandled char %c\n", c);
- }
- c = GETC();
- }
- return 0;
+ /* These conditions are ripped directly from SDCC.lex */
+ /* First check the parsing of the basic tokens */
+ TEST (altlex_testparse (">>=") == RIGHT_ASSIGN);
+ TEST (altlex_testparse ("<<=") == LEFT_ASSIGN);
+ TEST (altlex_testparse ("+=") == ADD_ASSIGN);
+ TEST (altlex_testparse ("-=") == SUB_ASSIGN);
+ TEST (altlex_testparse ("*=") == MUL_ASSIGN);
+ TEST (altlex_testparse ("/=") == DIV_ASSIGN);
+ TEST (altlex_testparse ("%=") == MOD_ASSIGN);
+ TEST (altlex_testparse ("&=") == AND_ASSIGN);
+ TEST (altlex_testparse ("^=") == XOR_ASSIGN);
+ TEST (altlex_testparse ("|=") == OR_ASSIGN);
+ TEST (altlex_testparse (">>") == RIGHT_OP);
+ TEST (altlex_testparse ("<<") == LEFT_OP);
+ TEST (altlex_testparse ("++") == INC_OP);
+ TEST (altlex_testparse ("--") == DEC_OP);
+ TEST (altlex_testparse ("->") == PTR_OP);
+ TEST (altlex_testparse ("&&") == AND_OP);
+ TEST (altlex_testparse ("||") == OR_OP);
+ TEST (altlex_testparse ("<=") == LE_OP);
+ TEST (altlex_testparse (">=") == GE_OP);
+ TEST (altlex_testparse ("==") == EQ_OP);
+ TEST (altlex_testparse ("!=") == NE_OP);
+ TEST (altlex_testparse (";") == ';');
+ TEST (altlex_testparse ("{") == '{');
+ TEST (altlex_testparse ("}") == '}');
+ TEST (altlex_testparse (",") == ',');
+ TEST (altlex_testparse (":") == ':');
+ TEST (altlex_testparse ("=") == '=');
+ TEST (altlex_testparse ("(") == '(');
+ TEST (altlex_testparse (")") == ')');
+ TEST (altlex_testparse ("[") == '[');
+ TEST (altlex_testparse ("]") == ']');
+ TEST (altlex_testparse (".") == '.');
+ TEST (altlex_testparse ("&") == '&');
+ TEST (altlex_testparse ("!") == '!');
+ TEST (altlex_testparse ("~") == '~');
+ TEST (altlex_testparse ("-") == '-');
+ TEST (altlex_testparse ("+") == '+');
+ TEST (altlex_testparse ("*") == '*');
+ TEST (altlex_testparse ("/") == '/');
+ TEST (altlex_testparse ("%") == '%');
+ TEST (altlex_testparse ("<") == '<');
+ TEST (altlex_testparse (">") == '>');
+ TEST (altlex_testparse ("^") == '^');
+ TEST (altlex_testparse ("|") == '|');
+ TEST (altlex_testparse ("?") == '?');
+
+ /* Now some character constants */
+ TEST (altlex_testchar ("'1'") == '1');
+ TEST (altlex_testchar ("'a'") == 'a');
+ TEST (altlex_testchar ("'A'") == 'A');
+ TEST (altlex_testchar ("'z'") == 'z');
+ TEST (altlex_testchar ("'Z'") == 'Z');
+ TEST (altlex_testchar ("'\n'") == '\n');
+ TEST (altlex_testchar ("'\\\\'") == '\\');
+ TEST (altlex_testchar ("'\\''") == '\'');
+
+ /* And some numbers */
+ TEST (altlex_testnum ("0") == 0);
+ TEST (altlex_testnum ("1") == 1);
+ TEST (altlex_testnum ("075") == 075);
+ TEST (altlex_testnum ("0xfeed") == 0xfeed);
+ TEST (altlex_testnum ("0xFEED") == 0xFEED);
+ TEST (altlex_testnum ("0x00005678") == 0x5678);
+
+ /* Keywords */
+ TEST (altlex_testparse ("auto") == AUTO);
+ TEST (altlex_testparse ("break") == BREAK);
+ TEST (altlex_testparse ("case") == CASE);
+ TEST (altlex_testparse ("char") == CHAR);
+ TEST (altlex_testparse ("const") == CONST);
+ TEST (altlex_testparse ("continue") == CONTINUE);
+ TEST (altlex_testparse ("default") == DEFAULT);
+ TEST (altlex_testparse ("do") == DO);
+ /* Prints a warning */
+ // TEST(altlex_testparse("double") == FLOAT);
+ TEST (altlex_testparse ("else") == ELSE);
+ TEST (altlex_testparse ("enum") == ENUM);
+ TEST (altlex_testparse ("extern") == EXTERN);
+ TEST (altlex_testparse ("float") == FLOAT);
+ TEST (altlex_testparse ("fixed16x16") == FIXED16X16);
+ TEST (altlex_testparse ("for") == FOR);
+ TEST (altlex_testparse ("goto") == GOTO);
+ TEST (altlex_testparse ("if") == IF);
+ TEST (altlex_testparse ("int") == INT);
+ TEST (altlex_testparse ("interrupt") == INTERRUPT);
+ TEST (altlex_testparse ("long") == LONG);
+ TEST (altlex_testparse ("register") == REGISTER);
+ TEST (altlex_testparse ("return") == RETURN);
+ TEST (altlex_testparse ("short") == SHORT);
+ TEST (altlex_testparse ("signed") == SIGNED);
+ TEST (altlex_testparse ("sizeof") == SIZEOF);
+ TEST (altlex_testparse ("static") == STATIC);
+ TEST (altlex_testparse ("struct") == STRUCT);
+ TEST (altlex_testparse ("switch") == SWITCH);
+ TEST (altlex_testparse ("typedef") == TYPEDEF);
+ TEST (altlex_testparse ("union") == UNION);
+ TEST (altlex_testparse ("unsigned") == UNSIGNED);
+ TEST (altlex_testparse ("void") == VOID);
+ TEST (altlex_testparse ("volatile") == VOLATILE);
+ TEST (altlex_testparse ("while") == WHILE);
+ TEST (altlex_testparse ("...") == VAR_ARGS);
+
+#if 0
+ /* Platform specific keywords */
+ TEST (altlex_testparse ("sram") ==)
+ {
+ count ();
+ TKEYWORD (XDATA);
+ }
+ TEST (altlex_testparse ("using") ==)
+ {
+ count ();
+ TKEYWORD (USING);
+ }
+ TEST (altlex_testparse ("near") ==)
+ {
+ count ();
+ TKEYWORD (DATA);
+ }
+ TEST (altlex_testparse ("at") ==)
+ {
+ count ();
+ TKEYWORD (AT);
+ }
+ TEST (altlex_testparse ("bit") ==)
+ {
+ count ();
+ TKEYWORD (BIT);
+ }
+ TEST (altlex_testparse ("code") ==)
+ {
+ count ();
+ TKEYWORD (CODE);
+ }
+ TEST (altlex_testparse ("critical") ==)
+ {
+ count ();
+ TKEYWORD (CRITICAL);
+ }
+ TEST (altlex_testparse ("data") ==)
+ {
+ count ();
+ TKEYWORD (DATA);
+ }
+ TEST (altlex_testparse ("far") ==)
+ {
+ count ();
+ TKEYWORD (XDATA);
+ }
+ TEST (altlex_testparse ("eeprom") ==)
+ {
+ count ();
+ TKEYWORD (EEPROM);
+ }
+ TEST (altlex_testparse ("flash") ==)
+ {
+ count ();
+ TKEYWORD (CODE);
+ }
+ TEST (altlex_testparse ("idata") ==)
+ {
+ count ();
+ TKEYWORD (IDATA);
+ }
+ TEST (altlex_testparse ("nonbanked") ==)
+ {
+ count ();
+ TKEYWORD (NONBANKED);
+ }
+ TEST (altlex_testparse ("banked") ==)
+ {
+ count ();
+ TKEYWORD (BANKED);
+ }
+ TEST (altlex_testparse ("pdata") ==)
+ {
+ count ();
+ TKEYWORD (PDATA);
+ }
+ TEST (altlex_testparse ("reentrant") ==)
+ {
+ count ();
+ TKEYWORD (REENTRANT);
+ }
+ TEST (altlex_testparse ("sfr") ==)
+ {
+ count ();
+ TKEYWORD (SFR);
+ }
+ TEST (altlex_testparse ("sbit") ==)
+ {
+ count ();
+ TKEYWORD (SBIT);
+ }
+ TEST (altlex_testparse ("xdata") ==)
+ {
+ count ();
+ TKEYWORD (XDATA);
+ }
+ TEST (altlex_testparse ("_data") ==)
+ {
+ count ();
+ TKEYWORD (_NEAR);
+ }
+ TEST (altlex_testparse ("_code") ==)
+ {
+ count ();
+ TKEYWORD (_CODE);
+ }
+ TEST (altlex_testparse ("_eeprom") ==)
+ {
+ count ();
+ TKEYWORD (_EEPROM);
+ }
+ TEST (altlex_testparse ("_flash") ==)
+ {
+ count ();
+ TKEYWORD (_CODE);
+ }
+ TEST (altlex_testparse ("_generic") ==)
+ {
+ count ();
+ TKEYWORD (_GENERIC);
+ }
+ TEST (altlex_testparse ("_near") ==)
+ {
+ count ();
+ TKEYWORD (_NEAR);
+ }
+ TEST (altlex_testparse ("_sram") ==)
+ {
+ count ();
+ TKEYWORD (_XDATA);
+ }
+ TEST (altlex_testparse ("_xdata") ==)
+ {
+ count ();
+ TKEYWORD (_XDATA);
+ }
+ TEST (altlex_testparse ("_pdata") ==)
+ {
+ count ();
+ TKEYWORD (_PDATA);
+ }
+ TEST (altlex_testparse ("_idata") ==)
+ {
+ count ();
+ TKEYWORD (_IDATA);
+ }
+#endif
+
+ return 0;