From d8b351472f8ec762141f0d172d4896daf54388ce Mon Sep 17 00:00:00 2001
From: Jake Wheat <jakewheatmail@gmail.com>
Date: Sun, 21 Feb 2016 23:36:47 +0200
Subject: [PATCH] add positional arg to the syntax and parser

---
 Language/SQL/SimpleSQL/Parse.lhs            | 13 +++++++++++++
 Language/SQL/SimpleSQL/Pretty.lhs           |  1 +
 Language/SQL/SimpleSQL/Syntax.lhs           |  1 +
 tools/Language/SQL/SimpleSQL/ValueExprs.lhs |  6 +++---
 4 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/Language/SQL/SimpleSQL/Parse.lhs b/Language/SQL/SimpleSQL/Parse.lhs
index 40e6f2c..70a120b 100644
--- a/Language/SQL/SimpleSQL/Parse.lhs
+++ b/Language/SQL/SimpleSQL/Parse.lhs
@@ -586,6 +586,11 @@ select x from t where x > :param
 >      <$> hostParamTok
 >      <*> optionMaybe (keyword "indicator" *> hostParamTok)]
 
+== positional arg
+
+> positionalArg :: Parser ValueExpr
+> positionalArg = PositionalArg <$> positionalArgTok
+
 == parens
 
 value expression parens, row ctor and scalar subquery
@@ -1238,6 +1243,7 @@ documenting/fixing.
 > term :: Parser ValueExpr
 > term = choice [simpleLiteral
 >               ,parameter
+>               ,positionalArg
 >               ,star
 >               ,parensExpr
 >               ,caseExpr
@@ -2048,6 +2054,13 @@ It is only allowed when all the strings are quoted with ' atm.
 >       L.PrefixedVariable c p -> Just (c:p)
 >       _ -> Nothing)
 
+> positionalArgTok :: Parser Int
+> positionalArgTok = mytoken (\tok ->
+>     case tok of
+>       L.PositionalArg p -> Just p
+>       _ -> Nothing)
+
+
 > sqlNumberTok :: Bool -> Parser String
 > sqlNumberTok intOnly = mytoken (\tok ->
 >     case tok of
diff --git a/Language/SQL/SimpleSQL/Pretty.lhs b/Language/SQL/SimpleSQL/Pretty.lhs
index 4df03bf..698c383 100644
--- a/Language/SQL/SimpleSQL/Pretty.lhs
+++ b/Language/SQL/SimpleSQL/Pretty.lhs
@@ -52,6 +52,7 @@ which have been changed to try to improve the layout of the output.
 > valueExpr _ (Iden i) = names i
 > valueExpr _ Star = text "*"
 > valueExpr _ Parameter = text "?"
+> valueExpr _ (PositionalArg n) = text $ "$" ++ show n
 > valueExpr _ (HostParameter p i) =
 >     text p
 >     <+> me (\i' -> text "indicator" <+> text i') i
diff --git a/Language/SQL/SimpleSQL/Syntax.lhs b/Language/SQL/SimpleSQL/Syntax.lhs
index f43ba58..17cb737 100644
--- a/Language/SQL/SimpleSQL/Syntax.lhs
+++ b/Language/SQL/SimpleSQL/Syntax.lhs
@@ -109,6 +109,7 @@
 >     | Star
 
 >     | Parameter -- ^ Represents a ? in a parameterized query
+>     | PositionalArg Int -- ^ Represents an e.g. $1 in a parameterized query
 >     | HostParameter String (Maybe String) -- ^ represents a host
 >                                           -- parameter, e.g. :a. The
 >                                           -- Maybe String is for the
diff --git a/tools/Language/SQL/SimpleSQL/ValueExprs.lhs b/tools/Language/SQL/SimpleSQL/ValueExprs.lhs
index 952b0e5..09d68e7 100644
--- a/tools/Language/SQL/SimpleSQL/ValueExprs.lhs
+++ b/tools/Language/SQL/SimpleSQL/ValueExprs.lhs
@@ -60,9 +60,9 @@ Tests for parsing value expressions
 >     ]
 
 > parameter :: TestItem
-> parameter = Group "parameter" $ map (uncurry (TestValueExpr ansi2011))
->     [("?", Parameter)
->     ]
+> parameter = Group "parameter"
+>     [TestValueExpr ansi2011 "?" Parameter
+>     ,TestValueExpr postgres "$13" $ PositionalArg 13]
 
 
 > dots :: TestItem