📄 jsgf.jj
字号:
<IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>MORE :{ < ~[] >}TOKEN : /* RESERVED WORDS AND LITERALS */{ < GRAMMAR: "grammar" >| < IMPORT: "import" >| < PUBLIC: "public" >}TOKEN : /* LITERALS */{ < INTEGER_LITERAL: <DECIMAL_LITERAL> >| < #DECIMAL_LITERAL: ["0"-"9"] (["0"-"9"])* >| < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])? | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])? | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])? | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"] >| < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >| < CHARACTER_LITERAL: "'" ( (~["'","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) ) "'" >| < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" >| < TAG: "{" ( (~["}"]) | ("\\" ( ["}","n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "}" >}TOKEN : /* IDENTIFIERS */{ < IDENTIFIER: (<LETTER>|<DIGIT>)+ >| < #LETTER: [ "-","'",":",",","\\","@","#","%","!", "^","&","~", "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] >| < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] >}RuleGrammar GrammarUnit(Recognizer R) :{ RuleGrammar G = null; }{ [IdentHeader()] G=GrammarDeclaration(R) ( ImportDeclaration(G) )* ( RuleDeclaration(G) )* <EOF> { return G; }}RuleGrammar GrammarDeclaration(Recognizer R) :{ String s; RuleGrammar G = null; Token t = null;}{ t=<GRAMMAR> s=Name() ";" { if (R != null) try { G=R.newRuleGrammar(s); } catch (IllegalArgumentException ge) { System.out.println("ERROR " + ge); }; if (G != null && G instanceof BaseRuleGrammar && t != null && t.specialToken != null) { if (t.specialToken.image != null && t.specialToken.image.startsWith("/**")) { BaseRuleGrammar JG = (BaseRuleGrammar) G; JG.addGrammarDocComment(t.specialToken.image); } } return G; }}void IdentHeader() :{}{ // oops this should be "#JSGF" <IDENTIFIER> "V1.0" [<IDENTIFIER> [<IDENTIFIER>]] ";"}void ImportDeclaration(RuleGrammar G) :{ boolean all = false; String name; Token t = null;}{ t=<IMPORT> "<" name=Name() [ "." "*" { all=true; }] ">" ";" { // import all rules if .* if (all) name = name + ".*"; RuleName r = new RuleName(name); if (G != null) { G.addImport(r); if (G instanceof BaseRuleGrammar && t != null && t.specialToken != null) { if (t.specialToken.image != null && t.specialToken.image.startsWith("/**")) { BaseRuleGrammar JG = (BaseRuleGrammar) G; JG.addImportDocComment(r, t.specialToken.image); } } } }}String Name() :/* * A lookahead of 2 is required below since "Name" can be followed * by a ".*" when used in the context of an "ImportDeclaration". */{ Token t1, t2; StringBuffer sb = new StringBuffer();}{ (t1=<IDENTIFIER> | t1=<PUBLIC> | t1=<IMPORT> | t1=<GRAMMAR>) { sb.append(t1.image); } ( LOOKAHEAD(2) "." t2=<IDENTIFIER> { sb.append('.'); sb.append(t2.image); } )* { return sb.toString(); }}void RuleDeclaration(RuleGrammar G) :{ boolean pub = false; String s; Rule r; Token t = null; Token t1 = null;}{ [t=<PUBLIC> { pub=true; }] t1="<" s=ruleDef() ">" "=" r=alternatives() ";" { try { if (G != null) { G.setRule(s,r,pub); if (G instanceof BaseRuleGrammar) { BaseRuleGrammar JG = (BaseRuleGrammar) G; String docComment = null; if ((t != null) && (t.specialToken != null) && (t.specialToken.image != null)) docComment = t.specialToken.image; else if ((t1 != null) && (t1.specialToken != null) && (t1.specialToken.image != null)) docComment = t1.specialToken.image; if (docComment != null && docComment.startsWith("/**")) { extractKeywords(JG,s,docComment); JG.addRuleDocComment(s,docComment); } JG.setSourceLine(s,t1.beginLine); } } } catch (IllegalArgumentException e) { System.out.println("ERROR SETTING RULE " + s); } }}RuleAlternatives alternatives() :{ Vector rv = new Vector(); Rule r; float w; float wa[] = new float[25]; int cnt=0;}{ ((r=sequence() { rv.addElement(r); } ("|" r=sequence() { rv.addElement(r); } )*) | (w=weight() r=sequence() { rv.addElement(r); wa[cnt++]=w; } ("|" w=weight() r=sequence() { rv.addElement(r); // make array bigger if needed if (cnt > (wa.length-1)) { float watmp[] = new float[wa.length+25]; System.arraycopy(wa,0,watmp,0,wa.length); wa = watmp; } wa[cnt++]=w; } )+)) { Rule rarry[] = new Rule[rv.size()]; rv.copyInto(rarry); RuleAlternatives ra = new RuleAlternatives(rarry); if (cnt != 0) { float wa1[] = new float[cnt]; System.arraycopy(wa,0,wa1,0,cnt); try { ra.setWeights(wa1); } catch (IllegalArgumentException e) { System.out.println("ERROR " + e); } } return ra; }}String ruleDef() : { Token t;}{ (t=<IDENTIFIER> | t=<INTEGER_LITERAL> | t=<PUBLIC> | t=<IMPORT> | t=<GRAMMAR>) { return t.image; }}RuleSequence sequence() :{ Rule r; Vector v = new Vector();}{ (r=item() { v.addElement(r); })+ { Rule ra[] = new Rule[v.size()]; v.copyInto(ra); return new RuleSequence(ra); }}float weight() :{ Token t; float f;}{ "/" (t=<FLOATING_POINT_LITERAL>|t=<INTEGER_LITERAL>) "/" { return Float.valueOf(t.image).floatValue(); }}Rule item() :{ Rule r; Vector t = null; int count=-1;}{ ( ((r=terminal() | r=ruleRef()) ["*" { count=RuleCount.ZERO_OR_MORE; } | "+" { count=RuleCount.ONCE_OR_MORE; }] [t=tag()]) | ("(" r=alternatives() ")" ["*" { count=RuleCount.ZERO_OR_MORE; } | "+" { count=RuleCount.ONCE_OR_MORE; }] [t=tag()]) | ("[" r=alternatives() "]" { count = RuleCount.OPTIONAL; } [t=tag()]) ) { if (count != -1) r = new RuleCount(r,count); if (t != null) { for (int i = 0; i < t.size(); i++) { String tag = (String) t.elementAt(i); if (tag.charAt(0) == '{') { tag = tag.substring(1,tag.length()-1); tag = tag.replace('\\',' '); } r = new RuleTag(r,tag); } } return r; }}Vector tag() :{ Token t; Vector v = new Vector();}{ (t=<TAG> { v.addElement(t.image); })+ { return v; }}Rule terminal() :{ Token t;}{ (t=<IDENTIFIER> | t=<STRING_LITERAL> | t=<INTEGER_LITERAL> | t=<PUBLIC> | t=<IMPORT> | t=<GRAMMAR>) { String tn = t.image; if (tn.startsWith("\"") && tn.endsWith("\"")) tn = tn.substring(1, tn.length()-1); RuleToken rt = new RuleToken(tn); return rt; }}RuleName ruleRef() :{ String s;}{ ("<" s=Name() ">") { RuleName rn = new RuleName(s); return rn; }}RuleName importRef() :{ String s; boolean all = false;}{ ("<" s=Name() [ "." "*" {all=true;}] ">") { if (all) s = s + ".*"; RuleName rn = new RuleName(s); return rn; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -