OmniSciDB  04ee39c94c
ExtensionFunctionSignatureParser.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.mapd.parser.server;
17 
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 
35  static Map<String, ExtensionFunction> parse(final String file_path) throws IOException {
36  File file = new File(file_path);
37  FileReader fileReader = new FileReader(file);
38  BufferedReader bufferedReader = new BufferedReader(fileReader);
39  String line;
40  Pattern r = Pattern.compile("([\\w]+) '([\\w]+) \\((.*)\\)'");
41  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
42  while ((line = bufferedReader.readLine()) != null) {
43  Matcher m = r.matcher(line);
44  if (m.find()) {
45  final String name = m.group(1);
46  final String ret = m.group(2);
47  final String cs_param_list = m.group(3);
48  sigs.put(name, toSignature(ret, cs_param_list));
49  }
50  }
51  return sigs;
52  }
53 
54  static Map<String, ExtensionFunction> parseFromString(final String udf_string)
55  throws IOException {
56  StringReader stringReader = new StringReader(udf_string);
57  BufferedReader bufferedReader = new BufferedReader(stringReader);
58  String line;
59  Pattern r = Pattern.compile("([\\w]+)\\s+'([\\w]+)\\s*\\((.*)\\)'");
60  Map<String, ExtensionFunction> sigs = new HashMap<String, ExtensionFunction>();
61  while ((line = bufferedReader.readLine()) != null) {
62  Matcher m = r.matcher(line);
63  if (m.find()) {
64  final String name = m.group(1);
65  final String ret = m.group(2);
66  final String cs_param_list = m.group(3);
67  sigs.put(name, toSignature(ret, cs_param_list));
68  }
69  }
70  return sigs;
71  }
72 
73  static String signaturesToJson(final Map<String, ExtensionFunction> sigs) {
74  List<String> json_sigs = new ArrayList<String>();
75  if (sigs != null) {
76  for (Map.Entry<String, ExtensionFunction> sig : sigs.entrySet()) {
77  json_sigs.add(sig.getValue().toJson(sig.getKey()));
78  }
79  }
80  return "[" + join(json_sigs, ",") + "]";
81  }
82 
84  final String ret, final String cs_param_list) {
85  String[] params = cs_param_list.split(",");
87  new ArrayList<ExtensionFunction.ExtArgumentType>();
88  for (final String param : params) {
89  final ExtensionFunction.ExtArgumentType arg_type = deserializeType(param.trim());
90  if (arg_type != ExtensionFunction.ExtArgumentType.Void) {
91  args.add(arg_type);
92  }
93  }
94  return new ExtensionFunction(args, deserializeType(ret));
95  }
96 
98  final String type_name) {
99  final String const_prefix = "const ";
100  if (type_name.startsWith(const_prefix)) {
101  return deserializeType(type_name.substring(const_prefix.length()));
102  }
103  if (type_name.equals("bool") || type_name.equals("_Bool")) {
105  }
106  if (type_name.equals("int8_t") || type_name.equals("char")
107  || type_name.equals("int8")) {
109  }
110  if (type_name.equals("int16_t") || type_name.equals("short")
111  || type_name.equals("int16")) {
113  }
114  if (type_name.equals("int32_t") || type_name.equals("int")
115  || type_name.equals("int32")) {
117  }
118  if (type_name.equals("int64_t") || type_name.equals("size_t")
119  || type_name.equals("int64") || type_name.equals("long")) {
121  }
122  if (type_name.equals("float") || type_name.equals("float32")) {
124  }
125  if (type_name.equals("double") || type_name.equals("float64")) {
127  }
128  if (type_name.isEmpty() || type_name.equals("void")) {
130  }
131  if (type_name.endsWith(" *")) {
132  return pointerType(deserializeType(type_name.substring(0, type_name.length() - 2)));
133  }
134  if (type_name.endsWith("*")) {
135  return pointerType(deserializeType(type_name.substring(0, type_name.length() - 1)));
136  }
137  assert false;
138  return null;
139  }
140 
142  final ExtensionFunction.ExtArgumentType targetType) {
143  switch (targetType) {
144  case Int8:
146  case Int16:
148  case Int32:
150  case Int64:
152  case Float:
154  case Double:
156  default:
157  assert false;
158  return null;
159  }
160  }
161 
162  static String join(final List<String> strs, final String sep) {
163  StringBuilder sb = new StringBuilder();
164  if (strs.isEmpty()) {
165  return "";
166  }
167  sb.append(strs.get(0));
168  for (int i = 1; i < strs.size(); ++i) {
169  sb.append(sep).append(strs.get(i));
170  }
171  return sb.toString();
172  }
173 }
static Map< String, ExtensionFunction > parseFromString(final String udf_string)
static ExtensionFunction.ExtArgumentType pointerType(final ExtensionFunction.ExtArgumentType targetType)
static ExtensionFunction toSignature(final String ret, final String cs_param_list)
static Map< String, ExtensionFunction > parse(final String file_path)
static String join(final List< String > strs, final String sep)
static String signaturesToJson(final Map< String, ExtensionFunction > sigs)
static ExtensionFunction.ExtArgumentType deserializeType(final String type_name)