CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.rubik.parser
Class ScriptParser

java.lang.Object
  extended by ch.randelshofer.rubik.parser.ScriptParser
Direct Known Subclasses:
KociembaENGParser

public class ScriptParser
extends java.lang.Object

Parser for rubik's cube scripts. The tokens and syntax-rules used by the parser are read from a Notation object.

The parser supports the EBNF ISO/IEC 14977 productions shown below. Alternative syntax-rules specifiable by the Notation object are indicated in square brackets. The syntax-rules are given here for readability. The syntax-rules actually implemented by the parser are more complex, because those must be LALR1 (left-aligned left recursive with a lookahead of 1) to be parseable. The rules used by the implementation are given in the comments of the methods.

 Sequence         = { Expression } ;

 Expression       = Construct [Operator Expression] ;

 Operator         = Commutator | Conjugator | Rotator ;

 Construct        = { Prefix }, Statement, {Suffix} ;

 Statement        = CompoundStatement | Move | NOP | Macro ;

 CompoundStatement= Grouping |
                    Inversion | Reflection |
                    Commutation | Conjugation | Rotation |
                    Permutation;

 Affix            = Construct ;
 Root             = Construct ;

 Grouping         = GroupingBegin, Sequence, GroupingEnd ;
 GroupingBegin    = Word ;
 GroupingEnd      = Word ;

 Inversion        = Inversion-prefix | Inversion-suffix |
                    Inversion-circumfix ;

 Inversion-prefix    = [InversionBegin], Invertor, [InversionEnd], Root ;
 Inversion-suffix    = Root, [InversionBegin], Invertor, [InversionEnd] ;
 Inversion-circumfix = InversionBegin, Root, InversionEnd ;
 Invertor         = Word ;
 InversionBegin   = Word ;
 InversionEnd     = Word ;

 Reflection       = Reflection-prefix | Reflection-suffix |
                    Reflection-circumfix ;

 Reflection-prefix = [ReflectionBegin], Reflector, [ReflectionEnd], Root ;
 Reflection-suffix = Root, [ReflectionBegin], Reflector, [ReflectionEnd] ;
 Reflection-circumfix = ReflectionBegin, Sequence, ReflectionEnd;
 Reflector        = Word ;
 ReflectionBegin  = Word ;
 ReflectionEnd    = Word ;

 Commutation      = Commutation-prefix | Commutation-suffix |
                    Commutation-precircumfix | Commutation-postcircumfix |
                    Commutation-preinfix | Commutation-postinfix ;

 Commutation-prefix        = CommutationBegin, Affix, CommutationEnd, Root ;
 Commutation-suffix        = Root, CommutationBegin, Affix, CommutationEnd ;
 Commutation-precircumfix  = CommutationBegin, Affix, CommutationDelim, Sequence, CommutationEnd ;
 Commutation-postcircumfix = CommutationBegin, Sequence CommutationDelim, Affix, CommutationEnd ;
 Commutation-preinfix      = Affix, CommutationDelim, Root ;
 Commutation-postinfix     = Root, CommutationDelim, Affix ;
 CommutationBegin = Word ;
 CommutationEnd   = Word ;
 CommutationDelim = Word ;

 Conjugation      = Conjugation-prefix | Conjugation-suffix |
                    Conjugation-precircumfix | Conjugation-postcircumfix |
                    Conjugation-preinfix | Conjugation-postinfix ;

 Conjugation-prefix        = ConjugationBegin, Affix, ConjugationEnd, Root ;
 Conjugation-suffix        = Root, ConjugationBegin, Affix, ConjugationEnd ;
 Conjugation-precircumfix  = ConjugationBegin, Affix, ConjugationDelim, Root, ConjugationEnd ;
 Conjugation-postcircumfix = ConjugationBegin, Root, ConjugationDelim, Affix, ConjugationEnd ;
 Conjugation-preinfix      = Affix, ConjugationDelim, Root ;
 Conjugation-postinfix     = Root, ConjugationDelim, Affix ;
 ConjugationBegin = Word ;
 ConjugationEnd   = Word ;
 ConjugationDelim = Word ;

 Rotation         = Rotation-prefix | Rotation-suffix |
                    Rotation-precircumfix | Rotation-postcircumfix |
                    Rotation-preinfix | Rotation-postinfix ;

 Rotation-prefix = RotationBegin, Affix, RotationEnd, Root ;
 Rotation-suffix = Root, RotationBegin, Affix, RotationEnd ;
 Rotation-precircumfix  = RotationBegin, Affix, RotationDelim, Sequence, RotationEnd ;
 Rotation-postcircumfix = RotationBegin, Sequence, RotationDelim, Affix, RotationEnd ;
 Rotation-preinfix      = Affix, RotationDelim, Root ;
 Rotation-postinfix     = Root, RotationDelim, Affix ;
 Rotator          = Script ;
 RotationBegin    = Word ;
 RotationEnd      = Word ;
 RotationDelim    = Word ;

 Permutation      = Permutation-prefix | Permutation-suffix |
                    Permutation-precircumfix | Permutation-postcircumfix ;

 Permutation-prefix        = [PermSign], PermBegin, { SidePerm | EdgePerm | CornerPerm }, PermEnd ;
 Permutation-suffix        = PermBegin, { SidePerm | EdgePerm | CornerPerm }, PermEnd, [PermSign] ;
 Permutation-precircumfix  = PermBegin, [PermSign], { SidePerm | EdgePerm | CornerPerm }, PermEnd ;
 Permutation-postcircumfix = PermBegin, { SidePerm | EdgePerm | CornerPerm }, [PermSign], PermEnd ;
 SidePerm-prefix           = [PermSign], Face, [Integer];
 SidePerm-suffix           = Face, [Integer], [PermSign] ;
 SidePerm-precircumfix     = Face, [Integer], [PermSign] ;
 SidePerm-postcircumfix    = [PermSign], Face, [Integer] ;
 EdgePerm         = Face, Face, [Integer];
 CornerPerm       = Face, Face, Face;
 Face             = Word;

 Move             = Word ;

 NOP              = Word ;

 Macro            = Word ;

 Word             = { letter | digit } ;
 Integer          = { digit } ;
 

Version:
10.0.1 2010-10-06 Fixes possible null pointer dereference in method parseExpressionAffix.
10.0 2009-07-31 Renamed EBNF elements to make intent clearer.
9.1 2009-04-11 Added method getNotation(). Fixed parsing of permutations for 6x6 and 7x7 cubes.
9.0 2009-01-22 Added support for infix syntax and for circumfix inversion and circumfix reflection.
8.0 2008-06-22 Added support for local macros.
7.3 2008-01-08 Added support for numbered parts in permutations.
7.2 2008-01-04 Added new constructor. Fixed parsing of permutations (in progress).
7.0 2007-06-16 Renamed "Twist" to "Move".
6.0 2005-12-24 Reworked.
3.1 2004-08-01 Permutations support now four different positions for the sign of a disjoint cycle.
3.0 2004-04-03 Support for Rotations, Two-Layer Twists, Slice Twists more flexible Permutation Syntax and Comments added. Childrens of a GroupingNode are now directly added to the GroupingNode instead of two a Node first.
2.3 2003-01-02 Grouping renamed to Grouping. Bug fixed when conjugator and/or commutator begin and end tokens are ambiguous with grouping begin and end tokens.
2.2 2002-12-26 EBNF ISO/IEC 14977 is now used to describe the supported syntax. Additional constants added to describe tokens. Attribute isPermutationSupporte added. Methode writeToken improved. Method isTwistSupported added.
2.1.1 2002-12-16 Method writeToken improved.
2.1 2002-02-04 Methods getEdgeTwistSymbol and getSideTwistSymbol added. 2.0 2001-07-25
Author:
Werner Randelshofer, Hausmatt 10, Immensee, CH-6405, Switzerland

Constructor Summary
ScriptParser(Notation notation)
          Creates a new ScriptParser.
ScriptParser(Notation notation, java.util.List<MacroNode> localMacros)
          Creates a ScriptParser for the specified notation and with the specified local macros.
 
Method Summary
 Notation getNotation()
           
 SequenceNode parse(java.io.Reader r)
          Parses a script.
 SequenceNode parse(java.io.Reader r, Node parent)
          Parses a script.
 SequenceNode parse(java.lang.String s)
          Parses a Script.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScriptParser

public ScriptParser(Notation notation)
Creates a new ScriptParser.

Parameters:
notation - The notation describing the tokens and the syntax of the scripts that can be parsed.

ScriptParser

public ScriptParser(Notation notation,
                    java.util.List<MacroNode> localMacros)
Creates a ScriptParser for the specified notation and with the specified local macros.

Parameters:
notation - The notation describing the tokens and the syntax of the scripts that can be parsed.
localMacros - Local Macros Entry.key = macro identifier, Entry.value = macro script.
Method Detail

getNotation

public Notation getNotation()

parse

public SequenceNode parse(java.lang.String s)
                   throws java.io.IOException
Parses a Script.
 Script = {Statement} ;
 
.

Throws:
java.io.IOException

parse

public SequenceNode parse(java.io.Reader r)
                   throws java.io.IOException
Parses a script.
 Script = {Statement} ;
 
.

Throws:
java.io.IOException

parse

public SequenceNode parse(java.io.Reader r,
                          Node parent)
                   throws java.io.IOException
Parses a script.
 Script = {Statement} ;
 
.

Throws:
java.io.IOException

(c) Werner Randelshofer.
All rights reserved.