[svn-inject] Installing original source of jabref
[debian/jabref.git] / src / java / net / sf / jabref / search / Parser.g
1 header {
2 package net.sf.jabref.search;
3 import java.io.StringReader;
4 }
5
6 class SearchExpressionParser extends Parser;
7
8 options {
9         importVocab = SearchExpressionLexer; // use vocab generated by lexer
10         exportVocab = SearchExpressionParser;
11         defaultErrorHandler = false;
12         buildAST = true;
13         k = 3;
14 }
15
16 tokens {
17         RegularExpression;
18         And;
19         Or;
20         Not;
21         ExpressionSearch;
22 }
23
24 // ---------- Java Source Code ----------
25
26 {
27         public boolean caseSensitive = false;
28     public boolean regex = true;
29         /** Creates a parser and lexer instance and tests the specified String.
30           * Returns the AST if s is in valid syntax for advanced field search, null otherwise. */
31         public static AST checkSyntax(String s, boolean caseSensitive, boolean regex) {
32                 // Is there some way to prevent instance creation here?
33                 // How can a parser and/or lexer be reused?
34                 SearchExpressionParser parser = new SearchExpressionParser(new SearchExpressionLexer(
35                                 new StringReader(s)));
36                 parser.caseSensitive = caseSensitive;
37                 parser.regex = regex;
38                 try {
39                         parser.searchExpression();
40                         return parser.getAST();
41                 } catch (Exception e) {
42                         return null;
43                 }
44         }
45 }
46
47 // ---------- Text and Regular Expressions ----------
48
49 quotedRegularExpression[boolean caseSensitive, boolean regex]:
50                 var_s:STRING
51                         {
52                                 ## = astFactory.make((new ASTArray(2)).add(new RegExNode(RegularExpression,var_s.getText(),caseSensitive,regex)).add(##));
53                         }
54                 ;
55
56 simpleRegularExpression[boolean caseSensitive, boolean regex]:
57                 var_s:FIELDTYPE
58                         {
59                                 ## = astFactory.make((new ASTArray(2)).add(new RegExNode(RegularExpression,var_s.getText(),caseSensitive,regex)).add(##));
60                         }
61                 ;
62
63 // ---------- Condition and Expressions ----------
64
65 searchExpression:
66         condition EOF;
67
68 condition:
69                 (expression LITERAL_and condition) => expression LITERAL_and! condition { ## = #( [And], ##); }
70                 |
71                 (expression LITERAL_or condition) => expression LITERAL_or! condition { ## = #( [Or], ##); }
72                 |
73                 expression // negation is done in expression
74                 ;
75
76 expression:
77                 expressionSearch
78                 |
79                 LPAREN! condition RPAREN!
80                 |
81                 LITERAL_not! expressionSearch { ## = #( [Not], ## ); }         // NOT single expression
82                 |
83                 LITERAL_not! LPAREN! condition RPAREN! { ## = #( [Not], ## ); } // NOT ( ... )
84                 ;
85
86 expressionSearch:
87                 quotedRegularExpression[false,true] compareType quotedRegularExpression[caseSensitive,regex]
88                         { ## = #( [ExpressionSearch], ## ); }
89                 |
90                 simpleRegularExpression[false,true] compareType quotedRegularExpression[caseSensitive,regex]
91                         { ## = #( [ExpressionSearch], ## ); }
92                 |
93                 simpleRegularExpression[false,true] compareType simpleRegularExpression[caseSensitive,regex]
94                         { ## = #( [ExpressionSearch], ## ); }
95                 |
96                 quotedRegularExpression[false,true] compareType simpleRegularExpression[caseSensitive,regex]
97                         { ## = #( [ExpressionSearch], ## ); }
98                 ;
99
100 compareType:
101                 LITERAL_contains | LITERAL_matches | EQUAL | EEQUAL | NEQUAL
102                 ;
103