Package org.h2.bnf
Class Bnf
- java.lang.Object
-
- org.h2.bnf.Bnf
-
public class Bnf extends java.lang.ObjectThis class can read a file that is similar to BNF (Backus-Naur form). It is made specially to support SQL grammar.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringcurrentTokenprivate java.lang.StringcurrentTopicprivate charfirstCharprivate intindexprivate RulelastRepeatprivate java.util.HashMap<java.lang.String,RuleHead>ruleMapThe rule map.private java.util.ArrayList<RuleHead>statementsprivate java.lang.Stringsyntaxprivate java.lang.String[]tokens
-
Constructor Summary
Constructors Constructor Description Bnf()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAlias(java.lang.String name, java.lang.String replacement)Add an alias for a rule.private voidaddFixedRule(java.lang.String name, int fixedType)private RuleHeadaddRule(java.lang.String topic, java.lang.String section, Rule rule)static BnfgetInstance(java.io.Reader csv)Create an instance using the grammar specified in the CSV file.java.util.HashMap<java.lang.String,java.lang.String>getNextTokenList(java.lang.String query)Get the list of tokens that can follow.RuleHeadgetRuleHead(java.lang.String title)Get the rule head for the given title.static java.lang.StringgetRuleMapKey(java.lang.String token)Convert convert ruleLink to rule_link.java.util.ArrayList<RuleHead>getStatements()Get the list of possible statements.static java.util.StringTokenizergetTokenizer(java.lang.String s)Get the tokenizer for the given syntax.voidlinkStatements()Cross-link all statements with each other.private voidparse(java.io.Reader reader)private RuleExtensionparseExtension(boolean compatibility)private RuleparseList()private RuleparseOr()private RuleparseRule()private RuleparseToken()private voidread()static booleanstartWithSpace(java.lang.String s)Check whether the statement starts with a whitespace.private java.lang.String[]tokenize()java.lang.StringtoString()voidupdateTopic(java.lang.String topic, DbContextRule rule)Update a topic with a context specific rule.voidvisit(BnfVisitor visitor, java.lang.String s)Parse the syntax and let the rule call the visitor.
-
-
-
Field Detail
-
ruleMap
private final java.util.HashMap<java.lang.String,RuleHead> ruleMap
The rule map. The key is lowercase, and all spaces are replaces with underscore.
-
syntax
private java.lang.String syntax
-
currentToken
private java.lang.String currentToken
-
tokens
private java.lang.String[] tokens
-
firstChar
private char firstChar
-
index
private int index
-
lastRepeat
private Rule lastRepeat
-
statements
private java.util.ArrayList<RuleHead> statements
-
currentTopic
private java.lang.String currentTopic
-
-
Method Detail
-
getInstance
public static Bnf getInstance(java.io.Reader csv) throws java.sql.SQLException, java.io.IOException
Create an instance using the grammar specified in the CSV file.- Parameters:
csv- if not specified, the help.csv is used- Returns:
- a new instance
- Throws:
java.sql.SQLException- on failurejava.io.IOException- on failure
-
addAlias
public void addAlias(java.lang.String name, java.lang.String replacement)Add an alias for a rule.- Parameters:
name- for example "procedure"replacement- for example "@func@"
-
addFixedRule
private void addFixedRule(java.lang.String name, int fixedType)
-
parse
private void parse(java.io.Reader reader) throws java.sql.SQLException, java.io.IOException- Throws:
java.sql.SQLExceptionjava.io.IOException
-
visit
public void visit(BnfVisitor visitor, java.lang.String s)
Parse the syntax and let the rule call the visitor.- Parameters:
visitor- the visitors- the syntax to parse
-
startWithSpace
public static boolean startWithSpace(java.lang.String s)
Check whether the statement starts with a whitespace.- Parameters:
s- the statement- Returns:
- if the statement is not empty and starts with a whitespace
-
getRuleMapKey
public static java.lang.String getRuleMapKey(java.lang.String token)
Convert convert ruleLink to rule_link.- Parameters:
token- the token- Returns:
- the rule map key
-
getRuleHead
public RuleHead getRuleHead(java.lang.String title)
Get the rule head for the given title.- Parameters:
title- the title- Returns:
- the rule head, or null
-
parseRule
private Rule parseRule()
-
parseOr
private Rule parseOr()
-
parseList
private Rule parseList()
-
parseExtension
private RuleExtension parseExtension(boolean compatibility)
-
parseToken
private Rule parseToken()
-
read
private void read()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
tokenize
private java.lang.String[] tokenize()
-
getNextTokenList
public java.util.HashMap<java.lang.String,java.lang.String> getNextTokenList(java.lang.String query)
Get the list of tokens that can follow. This is the main autocomplete method. The returned map for the query 'S' may look like this:key: 1#SELECT, value: ELECT key: 1#SET, value: ET
- Parameters:
query- the start of the statement- Returns:
- the map of possible token types / tokens
-
linkStatements
public void linkStatements()
Cross-link all statements with each other. This method is called after updating the topics.
-
updateTopic
public void updateTopic(java.lang.String topic, DbContextRule rule)Update a topic with a context specific rule. This is used for autocomplete support.- Parameters:
topic- the topicrule- the database context rule
-
getStatements
public java.util.ArrayList<RuleHead> getStatements()
Get the list of possible statements.- Returns:
- the list of statements
-
getTokenizer
public static java.util.StringTokenizer getTokenizer(java.lang.String s)
Get the tokenizer for the given syntax.- Parameters:
s- the syntax- Returns:
- the tokenizer
-
-