tribble.parse.sql
Class ExprLexer

java.lang.Object
  extended bytribble.parse.sql.ExprLexer

public abstract class ExprLexer
extends java.lang.Object

Utility methods for SQL-like query expression parsing.

Since:
2001-03-12
Version:
$Revision: 1.5 $ $Date: 2007/08/01 03:08:20 $
Author:
David R. Tribble (david@tribble.com).

Copyright ©2001 by David R. Tribble, all rights reserved.
Permission is granted to any person or entity except those designated by by the United States Department of State as a terrorist, or terrorist government or agency, to use and distribute this source code provided that the original copyright notice remains present and unaltered.


Method Summary
static int getToken(java.lang.String line, int pos, java.lang.StringBuffer tok)
          Extract the next word token from an SQL-like query expression.
static java.lang.String[] getTokens(java.lang.String line)
          Split an SQL-like query expression into separate word tokens.
static void main(java.lang.String[] args)
          Test driver for this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Test driver for this class.

Usage

java tribble.parse.sql.ExprLexer textline...

This splits the textline into separate tokens.

Parameters:
args - Command line arguments.
Throws:
java.lang.Exception - Thrown if an error occurs.
Since:
1.3, 2001-04-16

getToken

public static int getToken(java.lang.String line,
                           int pos,
                           java.lang.StringBuffer tok)
                    throws java.text.ParseException
Extract the next word token from an SQL-like query expression.

Words may be quoted by surrounding them with single quote (') or double quote (") characters. If a word contains either of these characters, it must be quoted, and the embedded quote characters must be preceded by a backslash character (\). (Embedded backslash characters should not be preceded by another backslash, however.)

Example

Consider the following input text line:

    Date\ 2 = "2001-01-01" & `I.D.` like '%\'s' & Rec.Sz >= 80
    0123456789_123456789_123456789_123456789_123456789_12345
 
The line above contains the following word tokens at the string offsets shown:
     0: "Date 2"
     5: "="
     7: "\"2001-01-01\""
    20: "&"
    22: "I.D."
    29: "like"
    34: "'%'s'"
    41: "&"
    43: "Rec"
    46: "."
    47: "Sz"
    50: ">="
    53: "80" 

Parameters:
line - A string containing one or more SQL-like query expression word tokens separated by whitespace.
pos - Position within line where token parsing is to begin.
tok - The text of the extracted word token is returned in this object.
Returns:
The position of the character following the last one parsed from line, or zero if there are no more word tokens to extract from the line. The returned value can serve as the starting parse position in a subsequent call to this method.
Throws:
java.text.ParseException - Thrown if a token is malformed, such as missing a closing quote.
Since:
1.1, 2001-03-12
See Also:
getTokens(java.lang.String)

getTokens

public static java.lang.String[] getTokens(java.lang.String line)
                                    throws java.text.ParseException
Split an SQL-like query expression into separate word tokens.

Words may be quoted by surrounding them with single quote (') or double quote (") characters. If a word contains either of these characters, it must be quoted, and the embedded quote characters must be preceded by a backslash character (\). (Embedded backslash characters should not be preceded by another backslash, however.)

Example

Consider the following input text line:

    Date\ 2 = "2001-01-01" & `I.D.` like '%\'s' & Rec.Sz >= 80
 
The line above is split into the following tokens:
    [0]:  "Date 2"
    [1]:  "="
    [2]:  "\"2001-01-01\""
    [3]:  "&"
    [4]:  "I.D."
    [5]:  "like"
    [6]:  "'%'s'"
    [7]:  "&"
    [8]:  "Rec"
    [9]:  "."
    [10]: "Sz"
    [11]: ">="
    [10]: "80" 

Parameters:
line - A string containing one or more text tokens separated by whitespace.
Returns:
An array of strings, representing the tokens extracted from line, or null if the input line is empty.
Throws:
java.text.ParseException - Thrown if a token is malformed, such as missing a closing quote.
Since:
1.1, 2001-03-12
See Also:
getToken(java.lang.String, int, java.lang.StringBuffer)