From 0008402349fc861a89632c9a07b73829a86aa9f9 Mon Sep 17 00:00:00 2001 From: hsz Date: Tue, 20 Jan 2015 12:55:30 +0100 Subject: [PATCH] better bnf and flex rules --- .gitignore | 2 +- resources/bnf/Latex.bnf | 3 +- src/mobi/hsz/idea/latex/lexer/Latex.flex | 58 ++++---- src/mobi/hsz/idea/latex/lexer/LatexLexer.java | 133 +++++++++--------- 4 files changed, 92 insertions(+), 104 deletions(-) diff --git a/.gitignore b/.gitignore index 0bf7db2..f47692d 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,7 @@ com_crashlytics_export_strings.xml ### idea-latex # Release package -idea-gitignore.jar +idea-latex.jar resources/templates.list build/ build.properties diff --git a/resources/bnf/Latex.bnf b/resources/bnf/Latex.bnf index dc45f19..fe97cd4 100644 --- a/resources/bnf/Latex.bnf +++ b/resources/bnf/Latex.bnf @@ -43,10 +43,11 @@ latexFile ::= item_ * private item_ ::= !<> expr * private expr ::= text_expr - | group_expr + | group_expr + | argument_expr | instruction_expr | special_expr + | TEXT private expr_inside ::= argument_expr | group_expr + | instruction_expr diff --git a/src/mobi/hsz/idea/latex/lexer/Latex.flex b/src/mobi/hsz/idea/latex/lexer/Latex.flex index dfb160f..37dcdfc 100644 --- a/src/mobi/hsz/idea/latex/lexer/Latex.flex +++ b/src/mobi/hsz/idea/latex/lexer/Latex.flex @@ -28,43 +28,37 @@ LINE_WS = [\ \t\f] WHITE_SPACE = ({LINE_WS}|{EOL})+ INSTRUCTION = \\[a-zA-Z]+ -ARGUMENT = [^\ \n\r\t\f]+ +//ARGUMENT = [^\ \n\r\t\f]+ COMMENT = %.* -SPECIAL = [\S] -CRLF = [\s\r\n]+ +ARGUMENT = [^\(\)\{\}\[\]\\,] +TEXT = [^\(\)\{\}\[\]\\\%\ \t\f\r\n]|"\\\%"|("\\"{SPECIAL}) +SPECIAL = "$"|"&"|"#"|"_"|"~"|"^"|"\\" -%state IN_GROUP +%state IN_ARGUMENT %% - { - {WHITE_SPACE}+ { return WHITE_SPACE; } - "{" { yypushback(1); yybegin(IN_GROUP); } - "[" { yypushback(1); yybegin(IN_GROUP); } - "(" { yypushback(1); yybegin(IN_GROUP); } +// brackets + "(" { yybegin(IN_ARGUMENT); return LPAREN; } + ")" { return RPAREN; } + "[" { yybegin(IN_ARGUMENT); return LBRACKET; } + "]" { return RBRACKET; } + "{" { yybegin(IN_ARGUMENT); return LBRACE; } + "}" { return RBRACE; } - "," { return COMMA; } - ":" { return COLON; } - "*" { return ASTERISK; } - "\\\\" { return LINE_BREAK; } - - {INSTRUCTION} { return INSTRUCTION; } - {COMMENT} { return COMMENT; } - {SPECIAL} { return SPECIAL; } - {CRLF} { return CRLF; } - - [^] { yybegin(YYINITIAL); return BAD_CHARACTER; } -} // - - { - {WHITE_SPACE}+ { yybegin(YYINITIAL); return CRLF; } + { + {ARGUMENT}+ { return ARGUMENT; } + "," { return COMMA; } + . { yypushback(1); yybegin(YYINITIAL); } +} - "{" { return LBRACE; } - "[" { return LBRACKET; } - "(" { return LPAREN; } - "}" { return RBRACE; } - "]" { return RBRACKET; } - ")" { return RPAREN; } +// special characters + ":" { return COLON; } + "*" { return ASTERISK; } + "\\\\" { return LINE_BREAK; } + {SPECIAL} { return SPECIAL; } - {ARGUMENT} { return ARGUMENT; } -} + {WHITE_SPACE}+ { return WHITE_SPACE; } + {INSTRUCTION} { return INSTRUCTION; } + {COMMENT} { return COMMENT; } + {TEXT}+ { return TEXT; } diff --git a/src/mobi/hsz/idea/latex/lexer/LatexLexer.java b/src/mobi/hsz/idea/latex/lexer/LatexLexer.java index 19ccb77..ef28d9f 100644 --- a/src/mobi/hsz/idea/latex/lexer/LatexLexer.java +++ b/src/mobi/hsz/idea/latex/lexer/LatexLexer.java @@ -1,4 +1,4 @@ -/* The following code was generated by JFlex 1.4.3 on 17.01.15 00:19 */ +/* The following code was generated by JFlex 1.4.3 on 20.01.15 12:54 */ package mobi.hsz.idea.latex.lexer; @@ -14,7 +14,7 @@ /** * This class is a scanner generated by * JFlex 1.4.3 - * on 17.01.15 00:19 from the specification file + * on 20.01.15 12:54 from the specification file * /home/hsz/Projects/idea-latex/src/mobi/hsz/idea/latex/lexer/Latex.flex */ public class LatexLexer implements FlexLexer { @@ -22,8 +22,8 @@ public class LatexLexer implements FlexLexer { private static final int ZZ_BUFFERSIZE = 16384; /** lexical states */ + public static final int IN_ARGUMENT = 2; public static final int YYINITIAL = 0; - public static final int IN_GROUP = 2; /** * ZZ_LEXSTATE[l] is the state in the DFA for the lexical state l @@ -39,10 +39,10 @@ public class LatexLexer implements FlexLexer { * Translates characters to character classes */ private static final String ZZ_CMAP_PACKED = - "\11\0\1\3\1\2\1\0\1\3\1\1\22\0\1\3\4\0\1\6"+ - "\2\0\1\13\1\21\1\16\1\0\1\14\15\0\1\15\6\0\22\5"+ - "\1\7\7\5\1\12\1\4\1\20\3\0\22\5\1\10\7\5\1\11"+ - "\1\0\1\17\uff82\0"; + "\11\0\1\1\1\2\1\0\1\1\1\1\22\0\1\1\2\0\1\10"+ + "\1\10\1\5\1\10\1\0\1\11\1\12\1\17\1\0\1\6\15\0"+ + "\1\16\6\0\32\4\1\13\1\3\1\14\1\10\1\10\1\0\32\4"+ + "\1\15\1\0\1\7\1\10\uff81\0"; /** * Translates characters to character classes @@ -55,12 +55,12 @@ public class LatexLexer implements FlexLexer { private static final int [] ZZ_ACTION = zzUnpackAction(); private static final String ZZ_ACTION_PACKED_0 = - "\2\0\1\1\2\2\1\1\1\3\1\4\1\5\1\6"+ + "\2\0\1\1\1\2\1\3\1\4\1\5\1\3\1\6"+ "\1\7\1\10\1\11\1\12\1\13\1\14\1\15\1\16"+ - "\1\17\1\20\1\21\1\22\1\23"; + "\1\17\1\0\1\20\1\21"; private static int [] zzUnpackAction() { - int [] result = new int[23]; + int [] result = new int[21]; int offset = 0; offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result); return result; @@ -85,12 +85,12 @@ private static int zzUnpackAction(String packed, int offset, int [] result) { private static final int [] ZZ_ROWMAP = zzUnpackRowMap(); private static final String ZZ_ROWMAP_PACKED_0 = - "\0\0\0\22\0\44\0\66\0\110\0\132\0\154\0\44"+ - "\0\176\0\44\0\44\0\44\0\44\0\220\0\242\0\220"+ - "\0\220\0\220\0\220\0\220\0\220\0\44\0\264"; + "\0\0\0\20\0\40\0\60\0\100\0\120\0\140\0\40"+ + "\0\140\0\140\0\140\0\140\0\140\0\40\0\40\0\160"+ + "\0\140\0\140\0\200\0\40\0\220"; private static int [] zzUnpackRowMap() { - int [] result = new int[23]; + int [] result = new int[21]; int offset = 0; offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result); return result; @@ -113,16 +113,17 @@ private static int zzUnpackRowMap(String packed, int offset, int [] result) { private static final int [] ZZ_TRANS = zzUnpackTrans(); private static final String ZZ_TRANS_PACKED_0 = - "\1\3\2\4\1\5\1\6\1\3\1\7\1\10\1\11"+ - "\3\12\1\13\1\14\1\15\3\3\1\16\3\17\5\16"+ - "\1\20\1\21\1\22\3\16\1\23\1\24\1\25\23\0"+ - "\2\4\1\5\4\0\1\11\12\0\3\5\22\0\1\26"+ - "\1\27\1\0\2\27\11\0\2\7\1\0\17\7\1\0"+ - "\2\11\5\0\1\11\11\0\1\16\3\0\16\16\1\0"+ - "\3\17\23\0\1\27\1\0\2\27\11\0"; + "\1\3\2\4\1\5\1\3\1\6\1\3\1\7\1\10"+ + "\1\11\1\12\1\13\1\14\1\15\1\16\1\17\3\20"+ + "\1\21\2\20\1\22\1\21\1\20\5\21\2\20\1\3"+ + "\2\0\1\23\1\3\1\0\1\3\1\0\1\3\5\0"+ + "\2\3\1\0\2\4\20\0\1\24\1\25\1\3\2\0"+ + "\1\3\7\0\2\6\1\0\15\6\20\0\3\20\1\0"+ + "\2\20\2\0\1\20\5\0\2\20\3\0\1\3\1\0"+ + "\1\3\2\0\1\3\13\0\1\25\13\0"; private static int [] zzUnpackTrans() { - int [] result = new int[198]; + int [] result = new int[160]; int offset = 0; offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result); return result; @@ -163,11 +164,11 @@ private static int zzUnpackTrans(String packed, int offset, int [] result) { private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute(); private static final String ZZ_ATTRIBUTE_PACKED_0 = - "\2\0\1\11\4\1\1\11\1\1\4\11\10\1\1\11"+ - "\1\1"; + "\2\0\4\1\1\11\1\1\5\11\3\1\2\11\1\0"+ + "\2\1"; private static int [] zzUnpackAttribute() { - int [] result = new int[23]; + int [] result = new int[21]; int offset = 0; offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result); return result; @@ -252,7 +253,7 @@ public LatexLexer(java.io.InputStream in) { char [] map = new char[0x10000]; int i = 0; /* index in packed string */ int j = 0; /* index in unpacked array */ - while (i < 66) { + while (i < 70) { int count = packed.charAt(i++); char value = packed.charAt(i++); do map[j++] = value; while (--count > 0); @@ -462,82 +463,74 @@ else if (zzAtEOF) { zzMarkedPos = zzMarkedPosL; switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) { - case 7: - { return COMMA; + case 10: + { yybegin(IN_ARGUMENT); return LBRACE; + } + case 18: break; + case 2: + { return WHITE_SPACE; + } + case 19: break; + case 9: + { return RBRACKET; } case 20: break; - case 4: - { return SPECIAL; + case 5: + { return RBRACE; } case 21: break; - case 3: - { return COMMENT; + case 17: + { return INSTRUCTION; } case 22: break; - case 18: - { return LINE_BREAK; + case 3: + { return SPECIAL; } case 23: break; - case 10: - { return ARGUMENT; + case 12: + { return ASTERISK; } case 24: break; - case 1: - { yybegin(YYINITIAL); return BAD_CHARACTER; + case 4: + { return COMMENT; } case 25: break; - case 8: - { return COLON; + case 15: + { return COMMA; } case 26: break; - case 12: - { return LBRACE; + case 1: + { return TEXT; } case 27: break; - case 14: - { return LPAREN; + case 11: + { return COLON; } case 28: break; - case 11: - { yybegin(YYINITIAL); return CRLF; + case 8: + { yybegin(IN_ARGUMENT); return LBRACKET; } case 29: break; case 6: - { yypushback(1); yybegin(IN_GROUP); + { yybegin(IN_ARGUMENT); return LPAREN; } case 30: break; case 13: - { return LBRACKET; + { return ARGUMENT; } case 31: break; - case 2: - { return WHITE_SPACE; + case 7: + { return RPAREN; } case 32: break; - case 17: - { return RPAREN; + case 16: + { return LINE_BREAK; } case 33: break; - case 15: - { return RBRACE; + case 14: + { yypushback(1); yybegin(YYINITIAL); } case 34: break; - case 9: - { return ASTERISK; - } - case 35: break; - case 5: - { return CRLF; - } - case 36: break; - case 19: - { return INSTRUCTION; - } - case 37: break; - case 16: - { return RBRACKET; - } - case 38: break; default: if (zzInput == YYEOF && zzStartRead == zzCurrentPos) { zzAtEOF = true;