File: elx-java.c

package info (click to toggle)
viewcvs 0.9.2%2Bcvs.1.0.dev.2004.07.28-4.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 1,452 kB
  • ctags: 1,355
  • sloc: python: 10,100; cpp: 840; ansic: 763; yacc: 526; sh: 163; makefile: 115
file content (148 lines) | stat: -rw-r--r-- 2,633 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>

#include "java.h"
#include "j_keywords.h"
#include "elx.h"

/* from j_scan.c */
extern int yylex(void);
extern void yylex_start(int *error_flag);
extern void yylex_finish(void);
extern const char *get_identifier(void);

static const char *fname;
static int saw_error = 0;

static int lineno = 1;
static int hpos = 1;
static int fpos = 0;

static int token_start = 0;
static int start_lineno;
static int start_hpos;

static elx_context_t *ectx;


//#define DEBUG_SCANNER

/* if we're debugging, then the scanner looks for this var */
int yysdebug = 0;

/* and the parser looks for this */
int yydebug = 1;


void yyserror(const char *msg)
{
    fprintf(stderr, "%s:%d:%d: lex error: %s\n",
            fname, start_lineno, start_hpos, msg);
    saw_error = 1;
}

void yyerror(const char *msg)
{
    fprintf(stderr, "%s:%d:%d: parse error: %s\n",
            fname, start_lineno, start_hpos, msg);
    saw_error = 1;
}

int yyslex(void)
{
    int c = fgetc(ectx->input_fp);

    if (c == EOF)
        return -1;      /* tell lexer we're done */

    ++fpos;
    if (c == '\n')
    {
        hpos = 1;
        ++lineno;
    }
    else
        ++hpos;

//    printf("char: '%c'\n", c);

    return c;
}

void issue_token(char which)
{
    const char *ident = NULL;

    if (ELX_DEFINES_SYM(which))
        ident = get_identifier();
    else
        ident = NULL;

    elx_issue_token(ectx, which, token_start, fpos - token_start + 1, ident);
}

void mark_token_start(void)
{
    token_start = fpos;
    start_lineno = lineno;
    start_hpos = hpos;
}

#ifdef DEBUG_SCANNER

void gen_scan_tokens(void)
{
    while (1)
    {
        int v = yylex();

        if (v == TK_IDENTIFIER)
            printf("%d-%d: %d '%s'\n",
                   token_start, fpos-1, v, get_identifier());
        else
            printf("%d-%d: %d\n", token_start, fpos-1, v);

        /* end of parse? */
        if (v <= 0)
            break;
    }
}

#else /* DEBUG_SCANNER */

static void gen_elx_tokens(void)
{
    /* ### what to do with the result? should have seen/set saw_error */
    (void) yyparse();
}

#endif /* DEBUG_SCANNER */

int main(int argc, const char **argv)
{
    int errcode;

    ectx = elx_process_args(argc, argv);

    yylex_start(&errcode);
    if (errcode)
    {
        fprintf(stderr, "error: yylex_start: %d\n", errcode);
        return EXIT_FAILURE;
    }

    elx_open_files(ectx);

#ifdef DEBUG_SCANNER
    gen_scan_tokens();
#else
    gen_elx_tokens();
#endif

    yylex_finish();
    elx_close_files(ectx);

    if (saw_error)
        return EXIT_FAILURE;
    return EXIT_SUCCESS;
}