Titan Core Initial Contribution
[deliverable/titan.core.git] / compiler2 / ttcn3 / ttcn3_preparser.l
1 /******************************************************************************
2 * Copyright (c) 2000-2014 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8 %option noyywrap
9 %option never-interactive
10 %option nounput
11
12 %{
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <errno.h>
18
19 #include "ttcn3_preparser.h"
20 #include "../../common/memory.h"
21 #include "../error.h"
22
23 /** This inefficient macro is needed to cope with binary files.
24 * The scanner must stop immediately when it encounters a zero byte,
25 * which might cause confusion in the internal algorithm of flex. */
26 #define YY_INPUT(buf,result,max_size) \
27 {\
28 int c = getc(yyin); \
29 if (c == EOF || c == '\0') result = YY_NULL; \
30 else { \
31 buf[0] = c; \
32 result = 1; \
33 } \
34 }
35
36 #define YY_DECL static int yylex(char **module_name)
37
38 %}
39
40 TTCN3MODULENAME [A-Za-z][A-Za-z0-9_]*
41 NEWLINE \r|\n|\r\n
42 WHITESPACE [ \t\v\f]
43 LINECOMMENT "//"[^\r\n]*{NEWLINE}
44
45 %x SC_blockcomment SC_ppdirective
46 %s SC_ttcn3module SC_ttcn3modulebody
47
48 %%
49 int blockcomment_caller = INITIAL, ppdirective_caller = INITIAL;
50 BEGIN(INITIAL);
51
52 <INITIAL,SC_ppdirective,SC_ttcn3module,SC_ttcn3modulebody>"/*" {
53 blockcomment_caller = YY_START;
54 BEGIN(SC_blockcomment);
55 }
56
57 ^{WHITESPACE}*"#" {
58 ppdirective_caller = YY_START;
59 BEGIN(SC_ppdirective);
60 }
61
62 {WHITESPACE}|{NEWLINE}|{LINECOMMENT}
63
64 <SC_blockcomment>
65 {
66 "*/" BEGIN(blockcomment_caller);
67 .|\n
68 }
69
70 <SC_ppdirective>
71 {
72 {NEWLINE} BEGIN(ppdirective_caller);
73 .|\\{NEWLINE}
74 }
75
76 <INITIAL>"module" BEGIN(SC_ttcn3module);
77
78 <SC_ttcn3module>{TTCN3MODULENAME} {
79 if (module_name != NULL) *module_name = mcopystr(yytext);
80 BEGIN(SC_ttcn3modulebody);
81 }
82
83 <SC_ttcn3modulebody>"{"|"."|"language" return 1;
84
85 . |
86 <*><<EOF>> return 0;
87
88 %%
89
90 int is_ttcn3_module(const char *file_name, FILE *fp, char **module_name)
91 {
92 int ret_val;
93 if (module_name != NULL) *module_name = NULL;
94 if (fseek(fp, 0L, SEEK_SET)) {
95 ERROR("Seeking to the beginning of file `%s' failed: %s", file_name,
96 strerror(errno));
97 errno = 0;
98 return 0;
99 }
100 yyin = fp;
101 ret_val = yylex(module_name);
102 yy_flush_buffer(YY_CURRENT_BUFFER);
103 if (ret_val == 0 && module_name != NULL && *module_name != NULL) {
104 Free(*module_name);
105 *module_name = NULL;
106 }
107 yylex_destroy();
108 return ret_val;
109 }
This page took 0.033768 seconds and 5 git commands to generate.