View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2004 Stephen F. K. Leach
5   // Copyright (C) 2004-2005 Kevin Rogers
6   //
7   // This file is part of MillScript.
8   //
9   // MillScript is free software; you can redistribute it and/or modify it under
10  // the terms of the GNU General Public License as published by the Free
11  // Software Foundation; either version 2 of the License, or (at your option)
12  // any later version.
13  //
14  // MillScript is distributed in the hope that it will be useful, but WITHOUT
15  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  // more details.
18  //
19  // You should have received a copy of the GNU General Public License along with
20  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
21  // Place, Suite 330, Boston, MA  02111-1307  USA
22  ////////////////////////////////////////////////////////////////////////////////
23  package org.millscript.millscript.syntax;
24  
25  import org.millscript.millscript.conf.Configuration;
26  
27  import java.io.Reader;
28  
29  //    Constants: string, number
30  //    Variables
31  //    RelOps:           < <= == >= >
32  //    Assignment:       x := E
33  //    Application:      f(x)
34  //    Conditionals:     if, unless
35  //    Loops:            for
36  
37  /**
38   * This class implements the MillScript parser.
39   */
40  public class ParserImpl extends ConfigurationParserImpl {
41  
42      //    Initializer
43      static {
44          // NOTE - WE DO NOT INCLUDE "(", "," or ")" HERE AS THEY ARE DEFINED IN
45          // THE CONFIGURATION PARSER. I'M SHOUTING FOR A GOOD REASON ;-)
46          //    Reserved
47          Syntax reserved = new ReservedSyntax();
48          put( "!!", reserved );
49          put( "present", reserved );
50          put( "nonAbsent", reserved );
51          put( "downto", reserved );
52          put( "endmethod", reserved );
53          put( "into", reserved );
54          put( "facet", reserved );
55          put( "as", reserved );
56  
57          //    Nonfix
58          Syntax nonfix = new NonfixSyntax();
59          put( "..", nonfix );
60          put( "=>", nonfix );
61          put( "/>", nonfix );
62          put( "</", nonfix );
63          put( "</", nonfix );
64          put( ";", nonfix );                //    Hmm, this might be a boo-boo.
65          put( "]", nonfix );
66          put( "}", nonfix );
67          put( ":", nonfix );
68          put( "case", nonfix );
69          put( "default", nonfix );
70          put( "do", nonfix );
71          put( "else", nonfix );
72          put( "elseif", nonfix );
73          put( "elseunless", nonfix );
74          put( "endclass", nonfix );
75          put( "enddefine", nonfix );
76          put( "endfunction", nonfix );
77          put( "endif", nonfix );
78          put( "endfor", nonfix );
79          put( "endfun", nonfix );
80          put( "endunless", nonfix );
81          put( "endwhile", nonfix );
82          put( "endswitch", nonfix );
83          put( "extends", nonfix );
84          put( "finally", nonfix );
85          put( "from", nonfix );
86          put( "in", nonfix );
87          put( "memo", nonfix );
88          put( "method", nonfix );
89          put( "override", reserved );
90          put( "slot", nonfix );
91          put( "then", nonfix );
92          put( "to", nonfix );
93          put( "where", nonfix );
94  
95          //    Prefix
96          put( "^", new ThisSyntax() );
97          put( "{", new ListSyntax() );
98          put( "<!--", new XMLCommentSyntax() );
99          put( "class", new SpiceClassSyntax( "endclass" ) );
100         put( "const", new VarSyntax( true ) );
101         put( "define", new DefineSyntax() );
102         put( "function", new FunctionSyntax( "endfunction" ) );
103         put( "for", new ForSyntax() );
104         put( "fun", new FunSyntax() );
105         put( "if", new ConditionalSyntax( "endif" ) );
106         put( "import", new ImportSyntax() );
107         put( "init", new InitSyntax() );
108         put( "none", new NoneSyntax() );
109         put( "not", new NotSyntax() );
110         put( "package", new PackageSyntax() );
111         put( "pragma", new PragmaSyntax() );
112         put( "unless", new ConditionalSyntax( "endunless" ) );
113         put( "switch", new SwitchSyntax() );
114         put( "val", new VarSyntax( true ) );
115         put( "var", new VarSyntax( false ) );
116         put( "while", new WhileSyntax() );
117 
118         //    Postfix/Infix
119         put( ":=", new AssignSyntax().setPrec( LOW_PREC + 100 ) );
120         put( "||", new OrAbsentSyntax().setPrec( LOW_PREC + 200 ) );
121         put( "&&", new AndAbsentSyntax().setPrec( LOW_PREC + 300 ) );
122         put( "or", new OrSyntax().setPrec( LOW_PREC + 400 ) );
123         put( "and", new AndSyntax().setPrec( LOW_PREC + 500 ) );
124         // put( "@", new DotSyntax().setPrec( COMMA_PREC + 100 ) );
125         put( "...", new ExplodeSyntax().setPrec( COMMA_PREC + 200 ) );
126         put( "++", new AppendSyntax().setPrec( COMMA_PREC + 300 ) );
127 
128         // Relational
129         Syntax relop = new RelationalOperationSyntax().setPrec( RELOP_PREC + 100 );
130         put( "<", new XMLElementSyntax().setPrec( RELOP_PREC + 100 ) );
131         put( "<=", relop );
132         put( "=", relop );
133         put( "/=", relop );
134         put( "==", relop );
135         put( "/==", relop );
136         put( ">", relop );
137         put( ">=", relop );
138         put( "@", new DotSyntax().setPrec( RELOP_PREC + 300 ) );
139 
140         // Arithmetic
141         Syntax a = new ArithSyntax().setPrec( ARITH_PREC + 100 );
142         Syntax b = new ArithSyntax().setPrec( ARITH_PREC + 200 );
143         put( "+", a );
144         put( "-", a );
145         put( "*", b );
146         put( "mod", b );
147         put( "div", b );
148 
149         // Other
150         Syntax s = new MapletSyntax().setPrec( TIGHT_PREC + 100 );
151         // put( "&", s );
152         put( "==>", s );
153         put( "[", new IndexSyntax().setPrec( TIGHT_PREC + 200 ) );
154         put( ".", new DotSyntax().setPrec( TIGHT_PREC + 300 ) );
155         put( "::", new QualifiedNameSyntax().setPrec( TIGHT_PREC + 500 ) );
156     }
157 
158     /**
159      * Constructs a new <code>ParserImpl</code> with the specified origin, source,
160      * interactive prompt and end of line comment status.
161      *
162      * @param origin    the origin message for this tokenizer
163      * @param r         the character source to tokenize
164      * @param eolc      flag indicating if end of line comments are supported
165      * @param c         the configuration
166      */
167     public ParserImpl( final String origin, final Reader r, final boolean eolc, final Configuration c ) {
168         super( origin, r, eolc, c );
169     }
170 
171 }