summaryrefslogtreecommitdiffstats
path: root/poxml/gettext.g
blob: 1e0a3b3412d829a2432a10d12c4e8f772bef5013 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

header "pre_include_hpp" {
#include <string>
using namespace std;
#include "parser.h"
}

options {
	language="Cpp";
}

{
#include <iostream>
#include "GettextLexer.h"
#include "GettextParser.h"
#include "antlr/AST.h"
#include "antlr/CommonAST.h"

/* 
int main()
{
	ANTLR_USING_NAMESPACE(std)
	ANTLR_USING_NAMESPACE(antlr)
	try {
		GettextLexer lexer(cin);
		GettextParser parser(lexer);
		parser.file();

	} catch(exception& e) {
		cerr << "exception: " << e.what() << endl;
	}
}
*/
}

class GettextParser extends Parser;

options {
	codeGenMakeSwitchThreshold = 3;
	codeGenBitsetTestThreshold = 4;
}

file returns [ MsgList ml ]
{
string c, mi, ms;
MsgBlock mb;
MsgList ml2;
}
 : (comment T_MSGID) => (mb=file_block ml2=file { ml = ml2; ml.append(mb); } )
 | (comment EOF) => c=comment { (void)c; }
 ;

file_block returns [ MsgBlock mb ]
{
string c, mi, mip, ms;
}
  : c=comment mi=msgid
  (
   ( ms=msgstr {
	mb.comment = QString::fromUtf8(c.c_str());
	mb.msgid = QString::fromUtf8(mi.c_str());
	mb.msgstr = QString::fromUtf8(ms.c_str());   
     }
   )
   |
   ( mip=msgid_plural ms=msgstr_plural {
	mb.comment = QString::fromUtf8(c.c_str());
	mb.msgid = QString::fromUtf8(mi.c_str());
	mb.msgid_plural = QString::fromUtf8(mip.c_str());
	mb.msgstr = QString::fromUtf8(ms.c_str());   
     }
   )
  )
  ;

comment returns [string s]
{
string r;
}
 : (c:T_COMMENT r=comment { s = c->getText() + r; } )
 | /* nothing */
 ;

msgid returns [string s]
 : T_MSGID t:T_STRING { s = t->getText(); }
 ;

msgid_plural returns [string s]
 : T_MSGID_PLURAL t:T_STRING { s = t->getText(); }
 ;

msgstr returns [string s]
 : T_MSGSTR t:T_STRING { s = t->getText(); }
 ;

msgstr_plural returns [string s]
 : (
    T_MSGSTR L_BRACKET n:T_INT R_BRACKET t:T_STRING { s = t->getText(); }
   )+
 ;

class GettextLexer extends Lexer;
options {
        charVocabulary = '\u0000'..'\u00FF';
        testLiterals=false;    // don't automatically test for literals
}

WS 
  : (' ' | '\t' 
  | ('\n' | "\r\n") { newline(); }
  ) { $setType(ANTLR_USE_NAMESPACE(antlr)Token::SKIP); }
  ;
  
L_BRACKET: '[' ;

R_BRACKET: ']' ;

T_INT : ( '0'..'9' )+
      ;
  
T_COMMENT : '#' (~'\n')*  
          ;

MSG_TAG : "msg" ( ("id") (
		    "" { $setType(T_MSGID); } 
		    | "_plural" { $setType(T_MSGID_PLURAL); }
		)
                | "str" { $setType(T_MSGSTR); }
                )
        ;

T_STRING 
	:	('"'! (ESC|~'"')* ('"'! (' ' | 't')*! '\n'! { newline(); } (' '! | '\t'!)*))+
	;

// copied from example
protected
ESC	:	'\\'
		(	'n'
		|	'r'
		|	't'
		|	'b'
		|	'f'
		|	'"'
		|	'\''
		|	'\\'
		|	('0'..'3')
			(
				options {
					warnWhenFollowAmbig = false;
				}
			:	('0'..'9')
				(	
					options {
						warnWhenFollowAmbig = false;
					}
				:	'0'..'9'
				)?
			)?
		|	('4'..'7')
			(
				options {
					warnWhenFollowAmbig = false;
				}
			:	('0'..'9')
			)?
		)
	;