diff options
Diffstat (limited to 'src/org/semanticweb/karma2/model/cqparser/ConjunctiveQuery.g')
| -rw-r--r-- | src/org/semanticweb/karma2/model/cqparser/ConjunctiveQuery.g | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/src/org/semanticweb/karma2/model/cqparser/ConjunctiveQuery.g b/src/org/semanticweb/karma2/model/cqparser/ConjunctiveQuery.g deleted file mode 100644 index 621b0ce..0000000 --- a/src/org/semanticweb/karma2/model/cqparser/ConjunctiveQuery.g +++ /dev/null | |||
| @@ -1,140 +0,0 @@ | |||
| 1 | grammar ConjunctiveQuery; | ||
| 2 | |||
| 3 | options { | ||
| 4 | language = Java; | ||
| 5 | output = AST; | ||
| 6 | } | ||
| 7 | |||
| 8 | tokens { | ||
| 9 | VARIABLE; | ||
| 10 | CONSTANT; | ||
| 11 | SCONSTANT; | ||
| 12 | ATOM; | ||
| 13 | HEADATOM; | ||
| 14 | PREDICATE; | ||
| 15 | ATOM_LIST; | ||
| 16 | TERM_LIST; | ||
| 17 | RULE; | ||
| 18 | EXPRESSION; | ||
| 19 | PREFIX_LIST; | ||
| 20 | ID; | ||
| 21 | PREFIX; | ||
| 22 | PREDICATE; | ||
| 23 | } | ||
| 24 | |||
| 25 | @header { | ||
| 26 | package org.semanticweb.karma2.model.cqparser; | ||
| 27 | |||
| 28 | import java.io.File; | ||
| 29 | import java.io.FileInputStream; | ||
| 30 | import java.io.InputStream; | ||
| 31 | import java.io.FileNotFoundException; | ||
| 32 | import java.io.IOException; | ||
| 33 | import java.util.Set; | ||
| 34 | |||
| 35 | import org.semanticweb.karma2.model.ConjunctiveQuery; | ||
| 36 | |||
| 37 | |||
| 38 | import org.semanticweb.karma2.model.cqparser.ConjunctiveQueryWalker; | ||
| 39 | import org.semanticweb.karma2.exception.IllegalInputQueryException; | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | @members{ | ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | public ConjunctiveQueryParser(String string) | ||
| 50 | throws FileNotFoundException, IOException { | ||
| 51 | this(new CommonTokenStream(new ConjunctiveQueryLexer(new ANTLRStringStream(string)))); | ||
| 52 | } | ||
| 53 | |||
| 54 | public ConjunctiveQueryParser(InputStream istream) throws FileNotFoundException, IOException { | ||
| 55 | this(new CommonTokenStream(new ConjunctiveQueryLexer(new ANTLRInputStream(istream)))); | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | public ConjunctiveQueryParser(File file) throws FileNotFoundException, IOException { | ||
| 61 | this(new CommonTokenStream(new ConjunctiveQueryLexer(new ANTLRInputStream(new FileInputStream(file))))); | ||
| 62 | |||
| 63 | } | ||
| 64 | |||
| 65 | public ConjunctiveQuery parse() throws IllegalInputQueryException { | ||
| 66 | cq_return r = null; | ||
| 67 | try { | ||
| 68 | r = cq(); | ||
| 69 | } catch (RecognitionException e) { | ||
| 70 | e.printStackTrace(); | ||
| 71 | } | ||
| 72 | CommonTree t = (CommonTree) r.getTree(); | ||
| 73 | |||
| 74 | //System.out.println(t.toStringTree()); | ||
| 75 | CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); | ||
| 76 | // AST nodes have payloads that point into token stream | ||
| 77 | nodes.setTokenStream(input); | ||
| 78 | |||
| 79 | |||
| 80 | ConjunctiveQueryWalker walker = new ConjunctiveQueryWalker(); | ||
| 81 | |||
| 82 | ConjunctiveQuery cq = walker.walkExpressionNode(t); | ||
| 83 | return cq; | ||
| 84 | } | ||
| 85 | |||
| 86 | public ConjunctiveQuery parseCQ() throws IllegalInputQueryException { | ||
| 87 | return parse(); | ||
| 88 | } | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | @lexer::header{ | ||
| 94 | package org.semanticweb.karma2.model.cqparser; | ||
| 95 | } | ||
| 96 | |||
| 97 | cq : | ||
| 98 | prefixlist rulebody -> ^(EXPRESSION prefixlist rulebody ); | ||
| 99 | |||
| 100 | prefixlist: | ||
| 101 | prefix (',' prefix)* -> ^(PREFIX_LIST prefix*); | ||
| 102 | |||
| 103 | prefix: | ||
| 104 | 'prefix' id ':' '<' url '>' -> ^(PREFIX id url); | ||
| 105 | |||
| 106 | |||
| 107 | rulebody: | ||
| 108 | headatom ('<-'|':') body '.'? -> ^(RULE headatom body); | ||
| 109 | |||
| 110 | body: | ||
| 111 | atom (',' atom)* -> ^(ATOM_LIST atom*); | ||
| 112 | |||
| 113 | |||
| 114 | headatom: | ||
| 115 | id '(' term (',' term)* ')' -> ^(HEADATOM term*); | ||
| 116 | |||
| 117 | atom: | ||
| 118 | compositeid '(' term (',' term)* ')' -> ^(ATOM compositeid term*); | ||
| 119 | |||
| 120 | compositeid: | ||
| 121 | (id) ':' (id) -> ^(ID id id); | ||
| 122 | |||
| 123 | |||
| 124 | term: | ||
| 125 | variable -> ^(VARIABLE variable) | ||
| 126 | | simpleid -> ^(SCONSTANT simpleid) | ||
| 127 | | compositeid -> ^(CONSTANT compositeid); | ||
| 128 | |||
| 129 | id : (STRING); | ||
| 130 | simpleid : '<' URLSTRING '>' | '<' STRING '>'; | ||
| 131 | |||
| 132 | // TODO: FIXIT X1 can be parsed as variable | ||
| 133 | variable: | ||
| 134 | ('?') id -> ^(id); | ||
| 135 | |||
| 136 | url : (URLSTRING); | ||
| 137 | |||
| 138 | URLSTRING : ('http://'|'file:/') ('a'..'z'|'A'..'Z'|'0'..'9'|'/'|'#'|'.'|'-'|'~'|'_')+; | ||
| 139 | STRING : ('a'..'z'|'A'..'Z'|'0'..'9'|'/'|'#'|'.'|'-'|'_')+; | ||
| 140 | WS : (' '|'\n'|'\r')+ {$channel=HIDDEN;} ; | ||
