1/*
2 * Copyright (C) 2009-2010 Julien BLACHE <jb@jblache.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19grammar DAAP;
20
21options {
22	output = AST;
23	ASTLabelType = pANTLR3_BASE_TREE;
24	language = C;
25}
26
27query	:	expr NEWLINE? EOF	->	expr
28	;
29	
30expr	:	aexpr (OPOR^ aexpr)*
31	;
32
33aexpr	:	crit (OPAND^ crit)*
34	;
35
36crit	:	LPAR expr RPAR		->	expr
37	|	STR
38	;
39
40QUOTE	:	'\'';
41LPAR	:	'(';
42RPAR	:	')';
43
44OPAND	:	'+';
45OPOR	:	',';
46
47NEWLINE	:	'\r'? '\n';
48
49/*
50Unescaping adapted from (ported to the C runtime)
51<http://stackoverflow.com/questions/504402/how-to-handle-escape-sequences-in-string-literals-in-antlr-3>
52*/
53STR
54@init{ pANTLR3_STRING unesc = GETTEXT()->factory->newRaw(GETTEXT()->factory); }
55	:	QUOTE ( reg = ~('\\' | '\'') { unesc->addc(unesc, reg); }
56			| esc = ESCAPED { unesc->appendS(unesc, GETTEXT()); } )+ QUOTE { SETTEXT(unesc); };
57
58fragment
59ESCAPED	:	'\\'
60		( '\\' { SETTEXT(GETTEXT()->factory->newStr8(GETTEXT()->factory, (pANTLR3_UINT8)"\\")); }
61		| '\'' { SETTEXT(GETTEXT()->factory->newStr8(GETTEXT()->factory, (pANTLR3_UINT8)"\'")); }
62		)
63	;
64