FLAME  devel
 All Classes Functions Variables Typedefs Enumerations Pages
test_lex.cpp
1 
2 #include <stdio.h>
3 
4 #include "glps_parser.h"
5 
6 #define YYSTYPE GLPS_STYPE
7 extern "C" {
8 // the generated headers wrap some parts in extern "C" blocks, but not all...
9 #include "glps.par.h"
10 #include "glps.tab.h"
11 }
12 
13 int main(int argc, char *argv[])
14 {
15  FILE *in = stdin;
16  if(argc>1) {
17  in = fopen(argv[1], "r");
18  if(!in) {
19  fprintf(stderr, "Failed to open %s\n", argv[1]);
20  return 2;
21  }
22  }
23 
24  parse_context ctxt;
25 
26  yyscan_t scanner;
27 
28  glps_lex_init_extra(&ctxt, &scanner);
29 
30  glps__switch_to_buffer(glps__create_buffer(in, 1024, scanner), scanner);
31 
32  int ret = 0;
33 
34  while(1) {
35  YYSTYPE lval;
36  int tok = glps_lex(&lval, scanner);
37  if(tok==0)
38  break;
39 
40  switch(tok) {
41  case NUM:
42  printf("Number: %g\n", lval.real);
43  break;
44  case STR:
45  printf("String: \"%s\"\n", lval.string->str.c_str());
46  delete lval.string;
47  break;
48  case KEYWORD:
49  printf("KW: %s\n", lval.string->str.c_str());
50  delete lval.string;
51  break;
52  case COMMENT:
53  break;
54  case NEG:
55  printf("NEG\n");
56  break;
57  //[=:;()\[\],+*/-]
58  case '=':
59  case ':':
60  case ';':
61  case '(':
62  case ')':
63  case '[':
64  case ']':
65  case ',':
66  case '+':
67  case '-':
68  case '*':
69  case '/':
70  printf("Literal: '%c'\n", tok);
71  break;
72 
73  default:
74  printf("Unknown token type '%c' (%d)\n", tok, tok);
75  ret = 1;
76  }
77  if(ret)
78  break;
79  }
80  printf("EOF\n");
81 
82  glps_lex_destroy(scanner);
83 
84  if(ctxt.last_error.size()) {
85  ret = 1;
86  fprintf(stderr, "Error: %s\n", ctxt.last_error.c_str());
87  }
88 
89  fclose(in);
90 
91  return ret;
92 }