Atitit. Annotation parsing (1)---------Lexical Analysis Attilax summary java. net
1. Application scenario::: Because to use the UI annotation 1
2. The process is as follows::: Lexical analysis (generate token flow) >>>> Grammar analysis (generate AST) >> explain execution ... 2
3. Lexical analysis of the framework, ANTLR 2
4. FSM state machine///Switch 2
5. The word stream must be recognized as reserved words, identifiers (variables), constants, operators (operator) and bounds of the five categories 2
5.1. Operator (operator)::: 2
5.2.4. The boundary character: ";" semicolon, "{}" curly braces, single quotes, double quotes 3
6. Define Context 3
7. Note Name Atstate processing 4
8. Split character processing Doublequoestate 5
9. Operator (operator): processing Leftbrackt 5
10. Call result, output token stream 6
GitHub Open Source Project Atiannointerpreter 7
1. Application scenario::: Because you want to use a UI annotation
String s = "@QueryAdptr (sqlwhere=\" clo1= ' @p ' ", prop2=\" v2\ ") @Nofilt";
Creating an environment
So, to parse the annotations ...
Online horse,, son can get the realization of the LAN ....
If the Java source annotation can be read by using the Java API ...
The annotations in HTML are implemented by the self-implementation LAN.
Author:: Old Wow's paw Attilax Airon, email:1466519819@qq.com
Reprint please indicate the source: Http://blog.csdn.net/attilax
2. The process is as follows::: Lexical analysis (generate token flow) >>>> Grammar analysis (generate AST) >> explain execution ...
3. Lexical analysis of the framework, ANTLR
Use tired hang, difficult. Son good, write his own blue ...
4. Fsm state machine///switch
Use an FSM to replace the switch mode ...
Oo language can also be done using state mode ...
5. Word flow must be recognized as reserved words, identifiers (variables), constants, operators (operators) and bounds of the five categories
5.1. operator (operator) :::
() []->.
? : |
Conditions |
From right to left |
() []->. |
Parentheses (functions, etc.), array, two struct members access |
From left to right |
, |
Commas (order) |
+ - |
Plus, minus |
From left to right |
Parentheses, spin brackets, equals
Reference
Compiler diy--Lexical analysis-godlike-Blog channel-CSDN.NET.htm
operator to use a state to describe ... 5.2 4. Operator: ";" semicolon, "{}" curly braces, single quotes, double quotes
The boundary character in the processing time son, Lin Bar filtration ...
6. Define context
Design Description::: Use keystate to define the operation state,,, keystate changes have state internal implementation
State uses to describe the status of each character ..., the modification of this condition has a context to implement.
Class Annopasercontext extends Context {
Holds an object instance of State type
public state State;
public state lastkeystate;
Public Object Rzt;
Public Token curtoken=new Token ();
Public list<token> tokenlist=new arraylist<token> ();
public Char Curchar;
Public char[] Chararr;
public int curcharindex;
public void SetState (state state) {
This.state = State;
}
/**
* User-Interested interface method
*/
public void request (String sampleparameter) {
State=new normalstate ();
if (Curcharindex>=sampleparameter.length ())
{
State=new finishstate ();
State.handle (Sampleparameter, this);
Return
}
Curchar=chararr[curcharindex];
def Todox if to swithc OAO
if (curchar== ' @ ')//keyword
State=new atstate ();
if (curchar== ' ")//splittor Word
State=new doublequoestate ();
if (curchar== ')//op Word
State=new Leftbrackt ();
if (curchar== ') ")
State=new Rightbrackt ();
if (curchar== ' = ')
State=new eqxstate ();
if (curchar== ', ')
State=new commastate ();
Transfer the state to handle
State.handle (Sampleparameter, this);
}
7. annotation name atstate processing
Atstate implements State {
@Override
public void handle (String Sampleparameter, context context) {
Annopasercontext ctt= (annopasercontext) context;
Char Curchar=ctt.curchar;
if (ctt.lastkeystate instanceof inistate | | ctt.lastkeystate instanceof rightbrackt)
{
Token tk=new Token ();
Tk.value=ctt.curtoken.value;
Ctt.tokenList.add (TK);
Ctt.curtoken=new Token ();
Ctt.lastkeystate=new atstate ();
}
Ctt.curtoken.value+=curchar;
ctt.curcharindex++;
} 8. Split character processing doublequoestate
Doublequoestate implements State {
@Override
public void handle (String Sampleparameter, context context) {
Annopasercontext ctt= (annopasercontext) context;
Char Curchar=ctt.curchar;
Token tk=new Token ();
Tk.value=ctt.curtoken.value;
Ctt.tokenList.add (TK);
Ctt.tokenList.add (New Token () {this.value= ",";}});
Ctt.curtoken=new Token ();
Ctt.curtoken.value+=curchar;
ctt.curcharindex++;
Ctt.lastkeystate=new doublequoestate ();
}
9. operator (operator): processing leftbrackt
Leftbrackt implements State {
@Override
public void handle (String Sampleparameter, context context) {
Annopasercontext ctt= (annopasercontext) context;
Char Curchar=ctt.curchar;
Token tk=new Token ();
Tk.value=ctt.curtoken.value;
Ctt.tokenList.add (TK);
Ctt.tokenList.add (New Token () {this.value= "(";}});
Ctt.curtoken=new Token ();
Ctt.curtoken.value+=curchar;
ctt.curcharindex++;
Ctt.lastkeystate=new Leftbrackt ();
}
10. Call result, output token stream
@QueryAdptr
(
Sqlwhere
=
clo1= ' @p '
,
Prop2
=
V2
)
@Nofilt
F