File: ttl.l

package info (click to toggle)
calf 0.0.19%2Bgit20140915%2B5de5da28-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,748 kB
  • ctags: 8,054
  • sloc: cpp: 32,680; xml: 10,722; ansic: 3,431; python: 1,537; makefile: 188; lex: 51; sh: 20
file content (51 lines) | stat: -rw-r--r-- 1,552 bytes parent folder | download | duplicates (4)
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
%{
#include <Python.h>
#include "ttldata.h"

int yyFlexLexer::yywrap() { return 1; }

void yyerror(const char *str)
{
    PyErr_SetString(PyExc_SyntaxError, str);
}

%}

%x C_COMMENT C_LONGSTRING C_STRING

SYMBOL [A-Za-z_-][a-zA-Z0-9_-]*
TRIPLEQUOTE \"\"\"

%%

@prefix { LEXER_DATA->add("prefix", "@prefix"); }
# BEGIN(C_COMMENT);
{TRIPLEQUOTE} { LEXER_DATA->strctx.clear(); BEGIN(C_LONGSTRING); }
\" { LEXER_DATA->strctx.clear(); BEGIN(C_STRING); }
\<[^>]*\> { LEXER_DATA->add("URI", std::string(yytext + 1, strlen(yytext) - 2)); }
[+-]?([0-9]+\.[0-9]*|\.[0-9]+)([eE][-+]?[0-9]+)? { std::stringstream ss(yytext); double value; ss >> value; LEXER_DATA->add("number", PyFloat_FromDouble(value)); }
[+-]?[0-9]+ { LEXER_DATA->add("number", PyInt_FromLong(atol(yytext))); }
{SYMBOL}?:{SYMBOL}? { LEXER_DATA->add("prnot", yytext); } 
{SYMBOL} { LEXER_DATA->add("symbol", yytext); } 
[.,;\[\]\(\)] { LEXER_DATA->add(yytext, yytext); } 
[ \t\n] ;

. { 
    std::stringstream ss;
    ss << "Unexpected characters: '" << yytext << "'" << std::endl;
    PyErr_SetString(PyExc_ValueError, ss.str().c_str());
    yyerror("Syntax error");
}

<C_LONGSTRING>{TRIPLEQUOTE} { LEXER_DATA->add("string", LEXER_DATA->strctx); BEGIN(INITIAL); }
<C_LONGSTRING>[^"]+ LEXER_DATA->strctx += yytext;
<C_LONGSTRING>\" LEXER_DATA->strctx += yytext;

<C_STRING>\" { LEXER_DATA->add("string", LEXER_DATA->strctx); BEGIN(INITIAL); }
<C_STRING>\\\" LEXER_DATA->strctx += "\""; 
<C_STRING>[^\\\"\n]+ LEXER_DATA->strctx += yytext;

<C_COMMENT>\n { BEGIN(INITIAL); }
<C_COMMENT>. ;

%%