Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updated version to support multiple threads accessing the parser

  • Loading branch information...
commit 5552eaa103ea3cfe12d70472095f38f1c5a2e13a 1 parent ee97b70
Antonio Garrote authored March 01, 2011
267  clj-sparql-test/sparql_parser.js
@@ -127,39 +127,47 @@ sparql_parser = (function(){
127 127
         var savedReportMatchFailures = context.reportMatchFailures;
128 128
         context.reportMatchFailures = false;
129 129
         var savedPos0 = pos;
130  
-        var result1 = parse_Prologue(context);
131  
-        if (result1 !== null) {
132  
-          var result6 = parse_SelectQuery(context);
133  
-          if (result6 !== null) {
134  
-            var result2 = result6;
  130
+        var result2 = parse_Prologue(context);
  131
+        if (result2 !== null) {
  132
+          var result7 = parse_SelectQuery(context);
  133
+          if (result7 !== null) {
  134
+            var result3 = result7;
135 135
           } else {
136  
-            var result5 = parse_ConstructQuery(context);
137  
-            if (result5 !== null) {
138  
-              var result2 = result5;
  136
+            var result6 = parse_ConstructQuery(context);
  137
+            if (result6 !== null) {
  138
+              var result3 = result6;
139 139
             } else {
140  
-              var result4 = parse_DescribeQuery(context);
141  
-              if (result4 !== null) {
142  
-                var result2 = result4;
  140
+              var result5 = parse_DescribeQuery(context);
  141
+              if (result5 !== null) {
  142
+                var result3 = result5;
143 143
               } else {
144  
-                var result3 = parse_AskQuery(context);
145  
-                if (result3 !== null) {
146  
-                  var result2 = result3;
  144
+                var result4 = parse_AskQuery(context);
  145
+                if (result4 !== null) {
  146
+                  var result3 = result4;
147 147
                 } else {
148  
-                  var result2 = null;;
  148
+                  var result3 = null;;
149 149
                 };
150 150
               };
151 151
             };
152 152
           }
153  
-          if (result2 !== null) {
154  
-            var result0 = [result1, result2];
  153
+          if (result3 !== null) {
  154
+            var result1 = [result2, result3];
155 155
           } else {
156  
-            var result0 = null;
  156
+            var result1 = null;
157 157
             pos = savedPos0;
158 158
           }
159 159
         } else {
160  
-          var result0 = null;
  160
+          var result1 = null;
161 161
           pos = savedPos0;
162 162
         }
  163
+        var result0 = result1 !== null
  164
+          ? (function(p, q) {
  165
+                return {token: 'query',
  166
+                        kind: 'query',
  167
+                        prologue: p,
  168
+                        units: [q]};
  169
+          })(result1[0], result1[1])
  170
+          : null;
163 171
         context.reportMatchFailures = savedReportMatchFailures;
164 172
         if (context.reportMatchFailures && result0 === null) {
165 173
           matchFailed("[2] Query");
@@ -6936,21 +6944,16 @@ sparql_parser = (function(){
6936 6944
 
6937 6945
         var savedReportMatchFailures = context.reportMatchFailures;
6938 6946
         context.reportMatchFailures = false;
6939  
-        var result4 = parse_Collection(context);
6940  
-        var result3 = result4 !== null
  6947
+        var result3 = parse_Collection(context);
  6948
+        var result2 = result3 !== null
6941 6949
           ? (function() {
6942 6950
                 return {token:"triplesnode", triplesContext:[], chainSubject:"todo"}
6943 6951
           })()
6944 6952
           : null;
6945  
-        if (result3 !== null) {
6946  
-          var result0 = result3;
  6953
+        if (result2 !== null) {
  6954
+          var result0 = result2;
6947 6955
         } else {
6948  
-          var result2 = parse_BlankNodePropertyList(context);
6949  
-          var result1 = result2 !== null
6950  
-            ? (function() {
6951  
-                  return {token:"triplesnode", triplesContext:[], chainSubject:"todo"}
6952  
-            })()
6953  
-            : null;
  6956
+          var result1 = parse_BlankNodePropertyList(context);
6954 6957
           if (result1 !== null) {
6955 6958
             var result0 = result1;
6956 6959
           } else {
@@ -6980,41 +6983,107 @@ sparql_parser = (function(){
6980 6983
         var savedReportMatchFailures = context.reportMatchFailures;
6981 6984
         context.reportMatchFailures = false;
6982 6985
         var savedPos0 = pos;
6983  
-        if (input.substr(pos, 1) === "[") {
6984  
-          var result1 = "[";
6985  
-          pos += 1;
6986  
-        } else {
6987  
-          var result1 = null;
6988  
-          if (context.reportMatchFailures) {
6989  
-            matchFailed(quoteString("["));
6990  
-          }
  6986
+        var result2 = [];
  6987
+        var result12 = parse_WS(context);
  6988
+        while (result12 !== null) {
  6989
+          result2.push(result12);
  6990
+          var result12 = parse_WS(context);
6991 6991
         }
6992  
-        if (result1 !== null) {
6993  
-          var result2 = parse_PropertyListNotEmpty(context);
6994  
-          if (result2 !== null) {
6995  
-            if (input.substr(pos, 1) === "]") {
6996  
-              var result3 = "]";
6997  
-              pos += 1;
6998  
-            } else {
6999  
-              var result3 = null;
7000  
-              if (context.reportMatchFailures) {
7001  
-                matchFailed(quoteString("]"));
7002  
-              }
  6992
+        if (result2 !== null) {
  6993
+          if (input.substr(pos, 1) === "[") {
  6994
+            var result3 = "[";
  6995
+            pos += 1;
  6996
+          } else {
  6997
+            var result3 = null;
  6998
+            if (context.reportMatchFailures) {
  6999
+              matchFailed(quoteString("["));
7003 7000
             }
7004  
-            if (result3 !== null) {
7005  
-              var result0 = [result1, result2, result3];
  7001
+          }
  7002
+          if (result3 !== null) {
  7003
+            var result4 = [];
  7004
+            var result11 = parse_WS(context);
  7005
+            while (result11 !== null) {
  7006
+              result4.push(result11);
  7007
+              var result11 = parse_WS(context);
  7008
+            }
  7009
+            if (result4 !== null) {
  7010
+              var result5 = parse_PropertyListNotEmpty(context);
  7011
+              if (result5 !== null) {
  7012
+                var result6 = [];
  7013
+                var result10 = parse_WS(context);
  7014
+                while (result10 !== null) {
  7015
+                  result6.push(result10);
  7016
+                  var result10 = parse_WS(context);
  7017
+                }
  7018
+                if (result6 !== null) {
  7019
+                  if (input.substr(pos, 1) === "]") {
  7020
+                    var result7 = "]";
  7021
+                    pos += 1;
  7022
+                  } else {
  7023
+                    var result7 = null;
  7024
+                    if (context.reportMatchFailures) {
  7025
+                      matchFailed(quoteString("]"));
  7026
+                    }
  7027
+                  }
  7028
+                  if (result7 !== null) {
  7029
+                    var result8 = [];
  7030
+                    var result9 = parse_WS(context);
  7031
+                    while (result9 !== null) {
  7032
+                      result8.push(result9);
  7033
+                      var result9 = parse_WS(context);
  7034
+                    }
  7035
+                    if (result8 !== null) {
  7036
+                      var result1 = [result2, result3, result4, result5, result6, result7, result8];
  7037
+                    } else {
  7038
+                      var result1 = null;
  7039
+                      pos = savedPos0;
  7040
+                    }
  7041
+                  } else {
  7042
+                    var result1 = null;
  7043
+                    pos = savedPos0;
  7044
+                  }
  7045
+                } else {
  7046
+                  var result1 = null;
  7047
+                  pos = savedPos0;
  7048
+                }
  7049
+              } else {
  7050
+                var result1 = null;
  7051
+                pos = savedPos0;
  7052
+              }
7006 7053
             } else {
7007  
-              var result0 = null;
  7054
+              var result1 = null;
7008 7055
               pos = savedPos0;
7009 7056
             }
7010 7057
           } else {
7011  
-            var result0 = null;
  7058
+            var result1 = null;
7012 7059
             pos = savedPos0;
7013 7060
           }
7014 7061
         } else {
7015  
-          var result0 = null;
  7062
+          var result1 = null;
7016 7063
           pos = savedPos0;
7017 7064
         }
  7065
+        var result0 = result1 !== null
  7066
+          ? (function(pl) {
  7067
+
  7068
+                GlobalBlankNodeCounter++;
  7069
+                var subject = {token:'blank', label:''+GlobalBlankNodeCounter};
  7070
+                var newTriples =  [];
  7071
+
  7072
+                for(var i=0; i< pl.pairs.length; i++) {
  7073
+                    var pair = pl.pairs[i];
  7074
+                    var triple = {}
  7075
+                    triple.subject = subject;
  7076
+                    triple.predicate = pair[0];
  7077
+                    triple.object = pair[1];
  7078
+                    newTriples.push(triple);
  7079
+                }
  7080
+
  7081
+                return {token: 'triplesnode',
  7082
+                        kind: 'blanknodepropertylist',
  7083
+                        triplesContext: pl.triplesContext.concat(newTriples),
  7084
+                        chainSubject: subject};
  7085
+          })(result1[3])
  7086
+          : null;
7018 7087
         context.reportMatchFailures = savedReportMatchFailures;
7019 7088
         if (context.reportMatchFailures && result0 === null) {
7020 7089
           matchFailed("[88] BlankNodePropertyList");
@@ -7038,38 +7107,82 @@ sparql_parser = (function(){
7038 7107
         var savedReportMatchFailures = context.reportMatchFailures;
7039 7108
         context.reportMatchFailures = false;
7040 7109
         var savedPos0 = pos;
7041  
-        if (input.substr(pos, 1) === "(") {
7042  
-          var result1 = "(";
7043  
-          pos += 1;
7044  
-        } else {
7045  
-          var result1 = null;
7046  
-          if (context.reportMatchFailures) {
7047  
-            matchFailed(quoteString("("));
7048  
-          }
  7110
+        var result1 = [];
  7111
+        var result12 = parse_WS(context);
  7112
+        while (result12 !== null) {
  7113
+          result1.push(result12);
  7114
+          var result12 = parse_WS(context);
7049 7115
         }
7050 7116
         if (result1 !== null) {
7051  
-          var result4 = parse_GraphNode(context);
7052  
-          if (result4 !== null) {
7053  
-            var result2 = [];
7054  
-            while (result4 !== null) {
7055  
-              result2.push(result4);
7056  
-              var result4 = parse_GraphNode(context);
7057  
-            }
  7117
+          if (input.substr(pos, 1) === "(") {
  7118
+            var result2 = "(";
  7119
+            pos += 1;
7058 7120
           } else {
7059 7121
             var result2 = null;
  7122
+            if (context.reportMatchFailures) {
  7123
+              matchFailed(quoteString("("));
  7124
+            }
7060 7125
           }
7061 7126
           if (result2 !== null) {
7062  
-            if (input.substr(pos, 1) === ")") {
7063  
-              var result3 = ")";
7064  
-              pos += 1;
7065  
-            } else {
7066  
-              var result3 = null;
7067  
-              if (context.reportMatchFailures) {
7068  
-                matchFailed(quoteString(")"));
7069  
-              }
  7127
+            var result3 = [];
  7128
+            var result11 = parse_WS(context);
  7129
+            while (result11 !== null) {
  7130
+              result3.push(result11);
  7131
+              var result11 = parse_WS(context);
7070 7132
             }
7071 7133
             if (result3 !== null) {
7072  
-              var result0 = [result1, result2, result3];
  7134
+              var result10 = parse_GraphNode(context);
  7135
+              if (result10 !== null) {
  7136
+                var result4 = [];
  7137
+                while (result10 !== null) {
  7138
+                  result4.push(result10);
  7139
+                  var result10 = parse_GraphNode(context);
  7140
+                }
  7141
+              } else {
  7142
+                var result4 = null;
  7143
+              }
  7144
+              if (result4 !== null) {
  7145
+                var result5 = [];
  7146
+                var result9 = parse_WS(context);
  7147
+                while (result9 !== null) {
  7148
+                  result5.push(result9);
  7149
+                  var result9 = parse_WS(context);
  7150
+                }
  7151
+                if (result5 !== null) {
  7152
+                  if (input.substr(pos, 1) === ")") {
  7153
+                    var result6 = ")";
  7154
+                    pos += 1;
  7155
+                  } else {
  7156
+                    var result6 = null;
  7157
+                    if (context.reportMatchFailures) {
  7158
+                      matchFailed(quoteString(")"));
  7159
+                    }
  7160
+                  }
  7161
+                  if (result6 !== null) {
  7162
+                    var result7 = [];
  7163
+                    var result8 = parse_WS(context);
  7164
+                    while (result8 !== null) {
  7165
+                      result7.push(result8);
  7166
+                      var result8 = parse_WS(context);
  7167
+                    }
  7168
+                    if (result7 !== null) {
  7169
+                      var result0 = [result1, result2, result3, result4, result5, result6, result7];
  7170
+                    } else {
  7171
+                      var result0 = null;
  7172
+                      pos = savedPos0;
  7173
+                    }
  7174
+                  } else {
  7175
+                    var result0 = null;
  7176
+                    pos = savedPos0;
  7177
+                  }
  7178
+                } else {
  7179
+                  var result0 = null;
  7180
+                  pos = savedPos0;
  7181
+                }
  7182
+              } else {
  7183
+                var result0 = null;
  7184
+                pos = savedPos0;
  7185
+              }
7073 7186
             } else {
7074 7187
               var result0 = null;
7075 7188
               pos = savedPos0;
BIN  java/SparqlParser.jar
Binary file not shown
20  java/sparqltest/SparqlParser.java
@@ -5,27 +5,15 @@
5 5
          System.loadLibrary("SparqlParserWrapper");
6 6
      }
7 7
 
8  
-    private native void init();
9 8
     private native String parse_query(String query);
10 9
 
11  
-    private static SparqlParser parser;
12  
-
13  
-    private SparqlParser() {
14  
-        init();
15  
-    }
16  
-
17  
-    public String _parse(String query){
18  
-        return parse_query(query);
19  
-    }
20  
-
21 10
     public static String parse(String query) {
22  
-        if(parser == null) {
23  
-            parser = new SparqlParser();
24  
-        }
25  
-        return parser._parse(query);
  11
+        return (new SparqlParser()).parse_query(query);
26 12
     }
27 13
 
28 14
     public static void main(String[] argv) {
29  
-        System.out.println("RESULT: "+SparqlParser.parse("SELECT * { ?s ?p ?o }"));
  15
+        String query = "BASE <a> PREFIX test: <b> INSERT DATA { <a> <b> \"c\" }";
  16
+        System.out.println("QUERY BEFORE:"+query);
  17
+        System.out.println("RESULT: "+SparqlParser.parse(query));
30 18
     }
31 19
  }
BIN  libSparqlParserWrapper.dylib
Binary file not shown
13  src/SparqlParser.c
@@ -4,17 +4,18 @@
4 4
 #include "sparqltest_SparqlParser.h"
5 5
 
6 6
 
7  
-SparqlParser parser;
8  
-
9  
-JNIEXPORT void JNICALL Java_sparqltest_SparqlParser_init (JNIEnv *env , jobject obj) {
10  
-  parser = SparqlParser();
11  
-}
12  
-
13 7
 JNIEXPORT jstring JNICALL Java_sparqltest_SparqlParser_parse_1query (JNIEnv *env, jobject obj, jstring javaQuery) {
  8
+  SparqlParser parser;
  9
+  parser = SparqlParser();
14 10
 
15 11
   const char *query = env->GetStringUTFChars(javaQuery, 0);
  12
+  //std::cout << "QUERY:";
  13
+  //std::cout << std::string(query);
  14
+
16 15
   std::string result = parser.parse(std::string(query));
17 16
 
  17
+  env->ReleaseStringUTFChars(javaQuery, query);
  18
+
18 19
   jstring javaResult = env->NewStringUTF(result.c_str());
19 20
 
20 21
   return javaResult;
7  src/sparqltest_SparqlParser.h
@@ -7,13 +7,6 @@
7 7
 #ifdef __cplusplus
8 8
 extern "C" {
9 9
 #endif
10  
-/*
11  
- * Class:     sparqltest_SparqlParser
12  
- * Method:    init
13  
- * Signature: ()V
14  
- */
15  
-JNIEXPORT void JNICALL Java_sparqltest_SparqlParser_init
16  
-  (JNIEnv *, jobject);
17 10
 
18 11
 /*
19 12
  * Class:     sparqltest_SparqlParser

0 notes on commit 5552eaa

Please sign in to comment.
Something went wrong with that request. Please try again.