summaryrefslogtreecommitdiff
path: root/src/include/parser
diff options
context:
space:
mode:
authorTom Lane2006-03-14 22:48:25 +0000
committerTom Lane2006-03-14 22:48:25 +0000
commit20ab467d76d78271006818d2baf4c9c8658d1f38 (patch)
tree7a536111b5cc4e494ac75558aad5655dfc8ab964 /src/include/parser
parent48fb696753e267447f99914c7968d0b4ffb5c5dc (diff)
Improve parser so that we can show an error cursor position for errors
during parse analysis, not only errors detected in the flex/bison stages. This is per my earlier proposal. This commit includes all the basic infrastructure, but locations are only tracked and reported for errors involving column references, function calls, and operators. More could be done later but this seems like a good set to start with. I've also moved the ReportSyntaxErrorPosition logic out of psql and into libpq, which should make it available to more people --- even within psql this is an improvement because warnings weren't handled by ReportSyntaxErrorPosition.
Diffstat (limited to 'src/include/parser')
-rw-r--r--src/include/parser/analyze.h9
-rw-r--r--src/include/parser/gramparse.h11
-rw-r--r--src/include/parser/parse_func.h5
-rw-r--r--src/include/parser/parse_node.h14
-rw-r--r--src/include/parser/parse_oper.h29
-rw-r--r--src/include/parser/parse_relation.h13
-rw-r--r--src/include/parser/parse_type.h8
7 files changed, 61 insertions, 28 deletions
diff --git a/src/include/parser/analyze.h b/src/include/parser/analyze.h
index 38e501875fe..57188b9fce9 100644
--- a/src/include/parser/analyze.h
+++ b/src/include/parser/analyze.h
@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/analyze.h,v 1.31 2006/03/05 15:58:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/parser/analyze.h,v 1.32 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -16,9 +16,10 @@
#include "parser/parse_node.h"
-extern List *parse_analyze(Node *parseTree, Oid *paramTypes, int numParams);
-extern List *parse_analyze_varparams(Node *parseTree, Oid **paramTypes,
- int *numParams);
+extern List *parse_analyze(Node *parseTree, const char *sourceText,
+ Oid *paramTypes, int numParams);
+extern List *parse_analyze_varparams(Node *parseTree, const char *sourceText,
+ Oid **paramTypes, int *numParams);
extern List *parse_sub_analyze(Node *parseTree, ParseState *parentParseState);
extern List *analyzeCreateSchemaStmt(CreateSchemaStmt *stmt);
extern void CheckSelectLocking(Query *qry, bool forUpdate);
diff --git a/src/include/parser/gramparse.h b/src/include/parser/gramparse.h
index 054604521bc..13af69116cd 100644
--- a/src/include/parser/gramparse.h
+++ b/src/include/parser/gramparse.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/gramparse.h,v 1.33 2006/03/07 01:00:18 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/parser/gramparse.h,v 1.34 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -17,6 +17,15 @@
#include "nodes/parsenodes.h"
+/*
+ * We track token locations in terms of byte offsets from the start of the
+ * source string, not the column number/line number representation that
+ * bison uses by default. Also, to minimize overhead we track only one
+ * location (usually the first token location) for each construct, not
+ * the beginning and ending locations as bison does by default. It's
+ * therefore sufficient to make YYLTYPE an int.
+ */
+#define YYLTYPE int
/* from scan.l */
extern void scanner_init(const char *str);
diff --git a/src/include/parser/parse_func.h b/src/include/parser/parse_func.h
index 82e114f6c80..13aa706a044 100644
--- a/src/include/parser/parse_func.h
+++ b/src/include/parser/parse_func.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/parse_func.h,v 1.54 2006/03/05 15:58:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/parser/parse_func.h,v 1.55 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -43,7 +43,8 @@ typedef enum
extern Node *ParseFuncOrColumn(ParseState *pstate,
List *funcname, List *fargs,
- bool agg_star, bool agg_distinct, bool is_column);
+ bool agg_star, bool agg_distinct, bool is_column,
+ int location);
extern FuncDetailCode func_get_detail(List *funcname, List *fargs,
int nargs, Oid *argtypes,
diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h
index 5262c804aba..2c23cf78946 100644
--- a/src/include/parser/parse_node.h
+++ b/src/include/parser/parse_node.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/parse_node.h,v 1.47 2006/03/05 15:58:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/parser/parse_node.h,v 1.48 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,6 +20,15 @@
/*
* State information used during parse analysis
*
+ * parentParseState: NULL in a top-level ParseState. When parsing a subquery,
+ * links to current parse state of outer query.
+ *
+ * p_sourcetext: source string that generated the raw parsetree being
+ * analyzed, or NULL if not available. (The string is used only to
+ * generate cursor positions in error messages: we need it to convert
+ * byte-wise locations in parse structures to character-wise cursor
+ * positions.)
+ *
* p_rtable: list of RTEs that will become the rangetable of the query.
* Note that neither relname nor refname of these entries are necessarily
* unique; searching the rtable by name is a bad idea.
@@ -53,6 +62,7 @@
typedef struct ParseState
{
struct ParseState *parentParseState; /* stack link */
+ const char *p_sourcetext; /* source text, or NULL if not available */
List *p_rtable; /* range table so far */
List *p_joinlist; /* join items so far (will become FromExpr
* node's fromlist) */
@@ -73,6 +83,8 @@ typedef struct ParseState
} ParseState;
extern ParseState *make_parsestate(ParseState *parentParseState);
+extern int parser_errposition(ParseState *pstate, int location);
+
extern Var *make_var(ParseState *pstate, RangeTblEntry *rte, int attrno);
extern Oid transformArrayType(Oid arrayType);
extern ArrayRef *transformArraySubscripts(ParseState *pstate,
diff --git a/src/include/parser/parse_oper.h b/src/include/parser/parse_oper.h
index f3c2ee543ec..33e54ef01e0 100644
--- a/src/include/parser/parse_oper.h
+++ b/src/include/parser/parse_oper.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/parse_oper.h,v 1.38 2006/03/05 15:58:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/parser/parse_oper.h,v 1.39 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -21,20 +21,27 @@
typedef HeapTuple Operator;
/* Routines to look up an operator given name and exact input type(s) */
-extern Oid LookupOperName(List *opername, Oid oprleft, Oid oprright,
- bool noError);
-extern Oid LookupOperNameTypeNames(List *opername, TypeName *oprleft,
- TypeName *oprright, bool noError);
+extern Oid LookupOperName(ParseState *pstate, List *opername,
+ Oid oprleft, Oid oprright,
+ bool noError, int location);
+extern Oid LookupOperNameTypeNames(ParseState *pstate, List *opername,
+ TypeName *oprleft, TypeName *oprright,
+ bool noError, int location);
/* Routines to find operators matching a name and given input types */
/* NB: the selected operator may require coercion of the input types! */
-extern Operator oper(List *op, Oid arg1, Oid arg2, bool noError);
-extern Operator right_oper(List *op, Oid arg, bool noError);
-extern Operator left_oper(List *op, Oid arg, bool noError);
+extern Operator oper(ParseState *pstate, List *op, Oid arg1, Oid arg2,
+ bool noError, int location);
+extern Operator right_oper(ParseState *pstate, List *op, Oid arg,
+ bool noError, int location);
+extern Operator left_oper(ParseState *pstate, List *op, Oid arg,
+ bool noError, int location);
/* Routines to find operators that DO NOT require coercion --- ie, their */
/* input types are either exactly as given, or binary-compatible */
-extern Operator compatible_oper(List *op, Oid arg1, Oid arg2, bool noError);
+extern Operator compatible_oper(ParseState *pstate, List *op,
+ Oid arg1, Oid arg2,
+ bool noError, int location);
/* currently no need for compatible_left_oper/compatible_right_oper */
@@ -55,9 +62,9 @@ extern Oid oprfuncid(Operator op);
/* Build expression tree for an operator invocation */
extern Expr *make_op(ParseState *pstate, List *opname,
- Node *ltree, Node *rtree);
+ Node *ltree, Node *rtree, int location);
extern Expr *make_scalar_array_op(ParseState *pstate, List *opname,
bool useOr,
- Node *ltree, Node *rtree);
+ Node *ltree, Node *rtree, int location);
#endif /* PARSE_OPER_H */
diff --git a/src/include/parser/parse_relation.h b/src/include/parser/parse_relation.h
index 4faa3351a5f..33ebad8abce 100644
--- a/src/include/parser/parse_relation.h
+++ b/src/include/parser/parse_relation.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/parse_relation.h,v 1.52 2006/03/05 15:58:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/parser/parse_relation.h,v 1.53 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -31,13 +31,15 @@ extern RangeTblEntry *GetRTEByRangeTablePosn(ParseState *pstate,
int varno,
int sublevels_up);
extern Node *scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
- char *colname);
-extern Node *colNameToVar(ParseState *pstate, char *colname, bool localonly);
+ char *colname, int location);
+extern Node *colNameToVar(ParseState *pstate, char *colname, bool localonly,
+ int location);
extern Node *qualifiedNameToVar(ParseState *pstate,
char *schemaname,
char *refname,
char *colname,
- bool implicitRTEOK);
+ bool implicitRTEOK,
+ int location);
extern RangeTblEntry *addRangeTableEntry(ParseState *pstate,
RangeVar *relation,
Alias *alias,
@@ -66,7 +68,8 @@ extern RangeTblEntry *addRangeTableEntryForJoin(ParseState *pstate,
extern void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte,
bool addToJoinList,
bool addToRelNameSpace, bool addToVarNameSpace);
-extern RangeTblEntry *addImplicitRTE(ParseState *pstate, RangeVar *relation);
+extern RangeTblEntry *addImplicitRTE(ParseState *pstate, RangeVar *relation,
+ int location);
extern void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up,
bool include_dropped,
List **colnames, List **colvars);
diff --git a/src/include/parser/parse_type.h b/src/include/parser/parse_type.h
index a7069ada910..62c02370deb 100644
--- a/src/include/parser/parse_type.h
+++ b/src/include/parser/parse_type.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/parser/parse_type.h,v 1.31 2006/03/05 15:58:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/parser/parse_type.h,v 1.32 2006/03/14 22:48:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,10 +20,10 @@
typedef HeapTuple Type;
-extern Oid LookupTypeName(const TypeName *typename);
+extern Oid LookupTypeName(ParseState *pstate, const TypeName *typename);
extern char *TypeNameToString(const TypeName *typename);
-extern Oid typenameTypeId(const TypeName *typename);
-extern Type typenameType(const TypeName *typename);
+extern Oid typenameTypeId(ParseState *pstate, const TypeName *typename);
+extern Type typenameType(ParseState *pstate, const TypeName *typename);
extern Type typeidType(Oid id);