com.hdcookbook.grin.io.text
Class ShowParser

java.lang.Object
  extended by com.hdcookbook.grin.io.text.ShowParser

public class ShowParser
extends java.lang.Object

The parser of a show file. This is a really simple-minded parser. For example, all tokens are just strings, so, for example, you have to write "( 0 3 )" and not "(0 3)", since the first has four tokens and the second only two.

Author:
Bill Foote (http://jovial.com)

Field Summary
static Command[] emptyCommandArray
          A useful constant for parsing: An empty command array
 
Constructor Summary
ShowParser(java.io.Reader reader, java.lang.String showName, SEShow show)
          Create a parser to parse a show at the given location.
ShowParser(java.io.Reader reader, java.lang.String showName, SEShow show, ShowBuilder builder)
          Create a parser to parse a show at the given location.
 
Method Summary
 void addForwardReference(ForwardReference fw, int rank)
          Adds a forward reference to a show feature.
 Assembly lookupAssemblyOrFail(java.lang.String name)
          Look up a named assembly.
 Feature[] lookupAssemblyParts(Assembly assembly, java.lang.String[] parts)
          Look up the parts of an assembly.
 Feature lookupFeatureOrFail(java.lang.String name)
          Look up a named feature.
 void parse()
          Parse the current show file.
 boolean parseBoolean()
          Parse a boolean value
 java.awt.Color parseColor()
          Parse a color representation ("{ r g b a }")
 java.awt.Color parseColorNoOpenBrace()
          Parse a color representation when the opening brace has already been read.
 Command[] parseCommands()
          Parse a list of commands
 Command[] parseCommandsNoOpenBrace()
          Parse a list of commands after the leading "{" has already been read
 void parseExpected(java.lang.String expected)
          Parses a token that we expect to see.
 java.lang.String[][] parseMatrix()
          Parse a 2-D matrix of strings.
static SEShow parseShow(java.lang.String showName, Director director, ShowBuilder builder)
          A utility method to convert text-based grin file to an SEShow object.
static SEShow parseShowNoAbort(java.lang.String showName, Director director, ShowBuilder builder)
          A utility method to convert text-based grin file to an SEShow object.
 java.lang.String[] parseStrings()
          Parse a list of strings
 java.lang.String[] parseStringsWithOpenBraceRead()
          Parse a list of strings, without reading the leading open-brace.
 void readJavaSource(java.lang.StringBuffer xletSource, java.lang.StringBuffer grinviewSource, java.lang.StringBuffer originalSource, SEShowCommand command)
          Read a java_source fragment.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

emptyCommandArray

public static final Command[] emptyCommandArray
A useful constant for parsing: An empty command array

Constructor Detail

ShowParser

public ShowParser(java.io.Reader reader,
                  java.lang.String showName,
                  SEShow show)
Create a parser to parse a show at the given location.

Parameters:
reader - Where to read the show from. We read it up to the end_show token. It is recommended to be a BufferedReader instance for a performance improvement.
showName - The name of the show, for error messages.
show - The show to populate. This should be a new, empty show.

ShowParser

public ShowParser(java.io.Reader reader,
                  java.lang.String showName,
                  SEShow show,
                  ShowBuilder builder)
Create a parser to parse a show at the given location.

Parameters:
reader - Where to read the show from. We read it up to the end_show token. It is recommended to be a BufferedReader instance for a performance improvement.
showName - The name of the show, for error messages.
show - The show to populate. This should be a new, empty show.
builder - A helper to build the tree. You can use something other than the default to add decorations to the tree, e.g. for debugging.
Method Detail

addForwardReference

public void addForwardReference(ForwardReference fw,
                                int rank)
Adds a forward reference to a show feature. The forward reference will be resolved after the entire show has been read. This is useful from an extension parser for looking up other parts of a show, like a named feature. You can use it like this:

     Lexer lexer = ...;
     final ShowParser parser = lexer.getParser();
     final MyFeature feature = ...;
     final String otherFeatureName = ...;
     ForwardReference fw = new ForwardReference(lexer) {
         public void resolve() throws IOException {
             Feature f = parser.lookupFeatureOrFail(otherFeatureName);
             feature.setOtherFeature(f);
         }
     };
     parser.addForwardReference(fw, 0);

 
The GRIN parser guarantees that all of its forward reference resolution will be copleted before the first forward reference added by this method. For example, all groups and assemblies will be completely populated before any of your forward references are.

Within your forward references, you might need to make sure that some of them are resolved before others. Within GRIN, and example of this is the visual RC handler, which depends on the assembly it's bound to being completely resolved before the RC handler's references are resolved. If you need to impose an ordering on the resolution order of different kinds of forward references, use the rank parameter.

Parameters:
fw - The forward reference to add
rank - The rank order. Higher numbered forward references are processed after lower numbered ones. Internally, the parser uses this as an array index. Must be >= 0.

parse

public void parse()
           throws java.io.IOException
Parse the current show file.

Throws:
java.io.IOException

parseCommands

public Command[] parseCommands()
                        throws java.io.IOException
Parse a list of commands

Throws:
java.io.IOException

parseCommandsNoOpenBrace

public Command[] parseCommandsNoOpenBrace()
                                   throws java.io.IOException
Parse a list of commands after the leading "{" has already been read

Throws:
java.io.IOException

lookupAssemblyParts

public Feature[] lookupAssemblyParts(Assembly assembly,
                                     java.lang.String[] parts)
                              throws java.io.IOException
Look up the parts of an assembly. This can be called from a forward reference.

Throws:
java.io.IOException
See Also:
ForwardReference

lookupFeatureOrFail

public Feature lookupFeatureOrFail(java.lang.String name)
                            throws java.io.IOException
Look up a named feature. Fail with an IOException if it's not found. This can be called from a forward reference.

Throws:
java.io.IOException
See Also:
ForwardReference

lookupAssemblyOrFail

public Assembly lookupAssemblyOrFail(java.lang.String name)
                              throws java.io.IOException
Look up a named assembly. Fail with an IOException if it's not found. This can be called from a forward reference.

Throws:
java.io.IOException
See Also:
ForwardReference

parseMatrix

public java.lang.String[][] parseMatrix()
                                 throws java.io.IOException
Parse a 2-D matrix of strings. It's not required to be rectangular; each row may have a different length.

Throws:
java.io.IOException

parseStrings

public java.lang.String[] parseStrings()
                                throws java.io.IOException
Parse a list of strings

Throws:
java.io.IOException

parseStringsWithOpenBraceRead

public java.lang.String[] parseStringsWithOpenBraceRead()
                                                 throws java.io.IOException
Parse a list of strings, without reading the leading open-brace.

Throws:
java.io.IOException

parseBoolean

public boolean parseBoolean()
                     throws java.io.IOException
Parse a boolean value

Throws:
java.io.IOException

parseColor

public java.awt.Color parseColor()
                          throws java.io.IOException
Parse a color representation ("{ r g b a }")

Throws:
java.io.IOException

parseColorNoOpenBrace

public java.awt.Color parseColorNoOpenBrace()
                                     throws java.io.IOException
Parse a color representation when the opening brace has already been read.

Throws:
java.io.IOException
See Also:
parseColor()

parseExpected

public void parseExpected(java.lang.String expected)
                   throws java.io.IOException
Parses a token that we expect to see. A token is read, and if it's not the expected token, an IOException is generated. This can be useful for things like parsing the ";" at the end of various constructs.

Throws:
java.io.IOException

readJavaSource

public void readJavaSource(java.lang.StringBuffer xletSource,
                           java.lang.StringBuffer grinviewSource,
                           java.lang.StringBuffer originalSource,
                           SEShowCommand command)
                    throws java.io.IOException
Read a java_source fragment. This is terminated with a "]]", which is consumed. This recognizes the special sequences XLET_ONLY_[[ java_source ]], GRINVIEW_ONLY_[[ java_source ]] and GRIN_COMMAND_[[ f ]]

Throws:
java.io.IOException

parseShow

public static SEShow parseShow(java.lang.String showName,
                               Director director,
                               ShowBuilder builder)
A utility method to convert text-based grin file to an SEShow object. Before calling this method, AssetFinder should be set with a proper search path for assets.

On a parse error, this prints an error message to stderr, then aborts execution. If you want the IOException yourself, then call parseShowNoAbort().

Parameters:
showName - The name of the show text file to parse.
director - The director to use for the recreated show, could be null.
builder - The show builder object with the extension parser being set, could be null.
Returns:
the SEShow object that got reconstructed.
See Also:
parseShowNoAbort(String, Director, ShowBuilder)

parseShowNoAbort

public static SEShow parseShowNoAbort(java.lang.String showName,
                                      Director director,
                                      ShowBuilder builder)
                               throws java.io.IOException
A utility method to convert text-based grin file to an SEShow object. Before calling this method, AssetFinder should be set with a proper search path for assets.

On a parse error, this throws an IO Exception, and doesn't print anything to stderr. If you want a more friendly error message, try parseShow().

Parameters:
showName - The name of the show text file to parse.
director - The director to use for the recreated show, could be null.
builder - The show builder object with the extension parser being set, could be null.
Returns:
the SEShow object that got reconstructed.
Throws:
java.io.IOException
See Also:
parseShow(String, Director, ShowBuilder)