From 9adce162e5c0ba25f2055c0c98f3592251ccd2db Mon Sep 17 00:00:00 2001
From: Jake Wheat <jakewheatmail@gmail.com>
Date: Mon, 15 Feb 2016 20:33:11 +0200
Subject: [PATCH] add support for [] quoted identifiers and for #var, @var

---
 Language/SQL/SimpleSQL/Lex.lhs              | 23 ++++++++++-----------
 tools/Language/SQL/SimpleSQL/LexerTests.lhs | 10 ++++++++-
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/Language/SQL/SimpleSQL/Lex.lhs b/Language/SQL/SimpleSQL/Lex.lhs
index e4ed1d3..20aa175 100644
--- a/Language/SQL/SimpleSQL/Lex.lhs
+++ b/Language/SQL/SimpleSQL/Lex.lhs
@@ -203,7 +203,12 @@ u&"unicode quoted identifier"
 >     ,Identifier (Just ("U&\"","\"")) <$> (try (string "U&") *> qiden)
 >     ,Identifier Nothing <$> identifierString
 >      -- todo: dialect protection
->     ,Identifier (Just ("`","`")) <$> mySqlQIden
+>     ,guard (diSyntaxFlavour d == MySQL) >>
+>      Identifier (Just ("`","`"))
+>      <$> (char '`' *> takeWhile1 (/='`') <* char '`')
+>     ,guard (diSyntaxFlavour d == SQLServer) >>
+>      Identifier (Just ("[","]"))
+>      <$> (char '[' *> takeWhile1 (`notElem` "[]") <* char ']')
 >     ]
 >   where
 >     qiden = char '"' *> qidenSuffix ""
@@ -215,10 +220,7 @@ u&"unicode quoted identifier"
 >                 void $ char '"'
 >                 qidenSuffix $ concat [t,s,"\"\""]
 >                ,return $ concat [t,s]]
->     -- mysql can quote identifiers with `
->     mySqlQIden = do
->         guard (diSyntaxFlavour d == MySQL)
->         char '`' *> takeWhile1 (/='`') <* char '`'
+
 
 This parses a valid identifier without quotes.
 
@@ -764,16 +766,13 @@ is an unambiguous parse
 TODO:
 
 refactor the tokenswillprintlex to be based on pretty printing the
- individual tokens
+  individual tokens
+make the tokenswill print more dialect accurate. Maybe add symbol
+  chars and identifier chars to the dialect definition and use them from
+  here
 
 start adding negative / different parse dialect tests
 
-lex @variable in sql server
-lex [quoted identifier] in sql server
-lex #variable in oracle
-
-make a new ctor for @var, #var
-
 add token tables and tests for oracle, sql server
 review existing tables
 
diff --git a/tools/Language/SQL/SimpleSQL/LexerTests.lhs b/tools/Language/SQL/SimpleSQL/LexerTests.lhs
index d85eb2d..bdcf9a8 100644
--- a/tools/Language/SQL/SimpleSQL/LexerTests.lhs
+++ b/tools/Language/SQL/SimpleSQL/LexerTests.lhs
@@ -16,6 +16,7 @@ Test for the lexer
 >                                ,postgresLexerTests
 >                                ,sqlServerLexerTests
 >                                ,oracleLexerTests
+>                                ,mySqlLexerTests
 >                                ,odbcLexerTests]]
 
 > ansiLexerTable :: [(String,[Token])]
@@ -283,7 +284,7 @@ the + or -.
 > sqlServerLexerTests = Group "sqlServerLexTests" $
 >     [ LexTest sqlserver s t | (s,t) <-
 >     [("@variable", [(PrefixedVariable '@' "variable")])
->     --,("[quoted identifier]", [(Identifier (Just ("[", "]")) "variable")])
+>     ,("[quoted identifier]", [(Identifier (Just ("[", "]")) "quoted identifier")])
 >     ]]
 
 > oracleLexerTests :: TestItem
@@ -293,6 +294,13 @@ the + or -.
 >     ]
 >     ]
 
+> mySqlLexerTests :: TestItem
+> mySqlLexerTests = Group "mySqlLexerTests" $
+>     [ LexTest mysql s t | (s,t) <-
+>     [("`quoted identifier`", [(Identifier (Just ("`", "`")) "quoted identifier")])
+>     ]
+>     ]
+
 > odbcLexerTests :: TestItem
 > odbcLexerTests = Group "odbcLexTests" $
 >     [ LexTest sqlserver {- {odbc = True} -} s t | (s,t) <-