File: txp.lex

package info (click to toggle)
tfortune 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: ansic: 2,477; makefile: 227; yacc: 85; sh: 25
file content (112 lines) | stat: -rw-r--r-- 2,771 bytes parent folder | download | duplicates (2)
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
/* SPDX-License-Identifier: GPL-3.0-only */

 /*
  * Since we do not supply yywrap(), we use noyywrap to instruct the scanner to
  * behave as though yywrap() returned 1.
  */
%option noyywrap

 /*
  * We don't want symbols to clash with those of other flex users, particularly
  * lopsub.
  */
%option prefix="txp_yy"

 /*
  * Generate a scanner that maintains the number of the current line read from
  * its input in the yylineno variable.
  */
%option yylineno

 /* Generate a bison-compatible scanner. */
%option bison-bridge bison-locations

 /*
  * Warn (in particular) if the default rule can be matched but no default rule
  * has been given.
  */
%option warn

 /*
  * Generate a scanner which is portable and safe to use in one or more threads
  * of control.
  */
%option reentrant

 /*
  * Generate a scanner which always looks one extra character ahead. This is a
  * bit faster than an interactive scanner for which look ahead happens only
  * when necessary.
  */
%option never-interactive

%{
#include "tf.h"

#define YYSTYPE TXP_YYSTYPE
#define YYLTYPE TXP_YYLTYPE
#define YY_DECL int txp_yylex(TXP_YYSTYPE *yylval_param, TXP_YYLTYPE *yylloc_param, \
	struct txp_context *ctx, struct txp_ast_node **ast, txp_yyscan_t yyscanner)
#include "txp.bison.h"
#define TXP_YY_USER_ACTION do {txp_yylloc->first_line = txp_yylineno;} while (0);
%}
DECIMAL_CONSTANT  (0|([[:digit:]]{-}[0])[[:digit:]]*)
STRING_LITERAL    \"([^\"\\\n]|(\\[\"\\abfnrtv]))*\"
REGEX_PATTERN     \/([^\/\\\n]|(\\[\/\\abfnrtv]))*\/([in])*
%%

tag {return TAG;}
len {return LEN;}
text {return TEXT;}
true {return TRUE;}
false {return FALSE;}

[[:space:]]+|#.*\n /* skip comments and whitespace */

"("|")"|","|"+"|"-"|"*"|"/"|"<"|">" {return yytext[0];}

"||" {return OR;}
"&&" {return AND;}
"!" {return NOT;}
"==" {return EQUAL;}
"!=" {return NOT_EQUAL;}
"<=" {return LESS_OR_EQUAL;}
">=" {return GREATER_OR_EQUAL;}
"=~" {return REGEX_MATCH;}

{DECIMAL_CONSTANT} {
	int ret;
	yylval->node = txp_new_ast_leaf_node(NUM);
	ret = atoi64(yytext, &yylval->node->sv.intval);
	if (ret < 0) {
		free(yylval->node);
		txp_parse_error(yylloc->first_line, ctx, "%s: %s", yytext,
			tf_strerror(-ret));
		return -E_TXP;
	}
	return NUM;
}

{STRING_LITERAL} {
	yylval->node = txp_new_ast_leaf_node(STRING_LITERAL);
	parse_quoted_string(yytext, "\"\"", &yylval->node->sv.strval);
	return STRING_LITERAL;
}

{REGEX_PATTERN} {
	int ret;
	yylval->node = txp_new_ast_leaf_node(REGEX_PATTERN);
	ret = txp_parse_regex_pattern(yytext, &yylval->node->sv.re_pattern);
	if (ret < 0) {
		txp_parse_error(yylloc->first_line, ctx, "%s: %s", yytext,
			tf_strerror(-ret));
		return -E_TXP;
	}
	return REGEX_PATTERN;
}

. {
	txp_parse_error(yylloc->first_line, ctx, "unrecognized text: %s",
		yytext);
	return -E_TXP;
}