Commits

Kota UENISHI committed c9e85cc

sample sql parser

Comments (0)

Files changed (5)

+.PHONY: all install clean
+
+USE_OCAMLFIND = true
+#
+# OCAMLPACKS[] =
+#    pack1
+#    pack2
+#
+if $(not $(OCAMLFIND_EXISTS))
+   eprintln(This project requires ocamlfind\, but is was not found.)
+   eprintln(You need to install ocamlfind and run "omake --configure".)
+   exit 1
+
+# OCAMLINCLUDES +=
+
+NATIVE_ENABLED = $(OCAMLOPT_EXISTS)
+BYTE_ENABLED = $(not $(OCAMLOPT_EXISTS))
+
+# OCAMLFLAGS    +=
+# OCAMLCFLAGS   +=
+# OCAMLOPTFLAGS +=
+# OCAML_LINK_FLAGS +=
+# OCAML_BYTE_LINK_FLAGS +=
+# OCAML_NATIVE_LINK_FLAGS +=
+
+OCamlGeneratedFiles(parser.ml lexer.ml)
+
+################################################
+# Build an OCaml library
+#
+
+# FILES[] =
+#    file1
+#    file2
+#
+# LIB = main
+#
+# .DEFAULT: $(OCamlLibrary $(LIB), $(FILES))
+
+################################################
+# Build an OCaml program
+#
+
+FILES[] =
+   sql
+#    file2
+#
+PROGRAM = sql
+# OCAML_LIBS +=
+# OCAML_CLIBS +=
+# OCAML_OTHER_LIBS +=
+# OCAML_LIB_FLAGS +=
+
+.DEFAULT: $(OCamlProgram $(PROGRAM), $(FILES))
+	./$(PROGRAM)
+########################################################################
+# Permission is hereby granted, free of charge, to any person
+# obtaining a copy of this file, to deal in the File without
+# restriction, including without limitation the rights to use,
+# copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the File, and to permit persons to whom the
+# File is furnished to do so, subject to the following condition:
+#
+# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR
+# THE USE OR OTHER DEALINGS IN THE FILE.
+
+########################################################################
+# The standard OMakeroot file.
+# You will not normally need to modify this file.
+# By default, your changes should be placed in the
+# OMakefile in this directory.
+#
+# If you decide to modify this file, note that it uses exactly
+# the same syntax as the OMakefile.
+#
+
+#
+# Include the standard installed configuration files.
+# Any of these can be deleted if you are not using them,
+# but you probably want to keep the Common file.
+#
+open build/C
+open build/OCaml
+open build/LaTeX
+
+#
+# The command-line variables are defined *after* the
+# standard configuration has been loaded.
+#
+DefineCommandVars()
+
+#
+# Include the OMakefile in this directory.
+#
+.SUBDIRS: .
+
+{
+
+open Parser
+let debugprint = ref false;;
+let print str =  if !debugprint then print_endline str;;
+
+}
+
+let digit = ['0'-'9']*
+let literal = ['a'-'z']['A'-'Z' 'a'-'z' '_' '0'-'9']*
+
+rule token = parse
+  | "select" { SELECT }
+  | "SELECT" { SELECT }
+  | "from" { FROM }
+  | "FROM" { FROM }
+  | ","    { COMMA }
+  | literal as s { LITERAL(s) }
+  | eof		{ print "eof."; EOF }
+  | _   { token lexbuf }
+
+%{
+
+  open Printf
+  let _ = Parsing.set_trace false;;
+  let debugprint = ref false;;
+  
+  let print str = if !debugprint then print_endline str;;
+  
+  let parse_error s = print ("parse_error->"^s);;
+
+%}
+
+%token SELECT
+%token INSERT
+%token DELETE
+%token UPDATE
+%token INTO
+%token VALUES
+%token SET
+%token FROM
+%token WHERE
+%token LIMIT
+%token DOT
+%token LBRACE RBRACE
+%token COMMA
+%token ASTERISK
+%token GT LT EQ NEQ NOT
+
+%token <int> INT
+
+%token <string> LITERAL
+
+
+%type <Sql.query> input
+
+%%
+
+input: | exp { $1 };
+
+exp:
+  | SELECT columns FROM TABLE { Sql.Select($2, $4) };
+
+columns:
+  | COLUMN { [$1] }
+  | COLUMN COMMA columns { $1::$3 }
+;
+
+type query = Select of string list * string (* columns, table *)
+
+let pp_query = function
+  | Select(columns, table) ->
+    Printf.printf "select: %s from %s\n" (String.concat "," columns) table;;
+
+let parse q =
+  let lexbuf = Lexing.from_string q in
+  try
+    Parser.input Lexer.token lexbuf
+  with
+    | exn ->
+      print_endline (Printexc.to_string exn);
+      raise exn;;
+
+let sample_query = "select * from mytable";;
+
+let _ =
+  print_endline sample_query;
+  pp_query (Select(["hoge"; "hage"], "tttttttttttttt"));
+  let _ = parse sample_query in
+  print_endline " => hello";;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.