pj
Class PJ

java.lang.Object
  extended by pj.PJ
All Implemented Interfaces:
Rfc.yyActions

public class PJ
extends Object
implements Rfc.yyActions

preprocessor for jay and JLex.

java -jar pj.jar [options] [--] [source|-]
options:
-d[ump] dump the parser tree
-f[latten] squeeze out a single ArrayList parameter
-j[ay][=file] postprocess with jay, keep jay input file
-p[ackages] delimeted-list packages to search for base classes, starts with delimeter
-r[ecover] generate error recovery for iterations
-s[canner][:s[tatic]][=file] - generate nested scanner class, static, keep JLex input file
-s[canner][:s[tatic]] file insert nested scanner class from JLex source file, static
-tra[ce][:0|1|2|3] postprocess with jay and compile jay trace, with animation simulating nothing, input, and/or output
-tre[e][:s[tatic]] - generate nested tree factory class, static
-tre[e] file only generate tree factory and insert into source file
-v[erbose] postprocess with jay and create parser description
-w[cs] allow System spoofing


Nested Class Summary
protected  class PJ.Action
          represents a right hand side which calls an action.
protected  class PJ.Collect
          represents a right hand side which collects one or more items.
protected  class PJ.Directive
          List<PJ.Info> base class for directives, flattens content.
protected  class PJ.ErrorInfo
          represents error.
protected  class PJ.IdInfo
          represents a jay terminal or non-terminal.
protected  class PJ.Info
          common information.
protected  class PJ.Left
          represents %left, flattens content.
protected  class PJ.Lit1Info
          represents a (potentially) screened literal input string of length one.
protected  class PJ.LitInfo
          represents a screened literal input string.
protected  class PJ.MacInfo
          represents a JLex macro.
protected  class PJ.Many
          represents id *.
protected  class PJ.Nonassoc
          represents %nonassoc, flattens content.
protected  class PJ.Opt
          represents id ?
protected  class PJ.Parser
          represents the top level.
protected  class PJ.Repeat
          base class for iterative constructs.
protected  class PJ.Rhs
          List<Leaf> represents one right hand side of a PJ.Rule, flattens.
protected  class PJ.Right
          represents %right, flattens content.
protected  class PJ.Rule
          List<PJ.Rhs> represents a rule, flattens content.
protected  class PJ.Scan
          List<PJ.SymInfo> represents %scan, flattens content.
protected  class PJ.Screen
          List<PJ.MacInfo> represents %screen, flattens content.
protected  class PJ.Sep
          represents id/operator.
protected  class PJ.Skip
          List<PJ.MacInfo> represents %skip, flattens content.
protected  class PJ.Some
          represents id +.
protected  class PJ.SymInfo
          represents a literal input string.
protected  class PJ.Token
          List<PJ.IdInfo> represents %token, flattens content.
 
Field Summary
protected  HashMap actions
          maps action header to return type, avoids duplicate action signatures and inconsistent return types.
protected  HashSet classes
          avoids duplicate classes.
protected  PJ.ErrorInfo errorInfo
          singleton representing error.
protected  HashMap ids
          maps ids to one-element arrays of PJ.IdInfo structures.
protected  String javaLangSystem
          classname to use for System.
protected  boolean literalsUsed
          true if long literals were used.
protected  HashMap lits
          maps lits to one-element arrays of PJ.LitInfo or PJ.Lit1Info structures.
protected  HashMap macs
          maps macro names to one-element arrays of PJ.MacInfo structures.
protected  String[] prefixes
          prefixes to try to locate a base class.
protected  Rfc rfc
          permits access to Rfc.yyerror(java.lang.String).
protected  boolean screened
          true if %screen was specified.
protected  boolean skipped
          true if %skip was specified.
protected  HashMap syms
          maps syms to one-element arrays of PJ.SymInfo structures.
static ResourceBundle templates
          MessageFormat.format templates to generate output and other configuration parameters.
protected  boolean typed
          true if type or action was specified.
protected  int uniq
          suffix for unique sym and lit names.
 
Constructor Summary
PJ(Rfc rfc, String prefixes, String javaLangSystem)
          remember rfc for error messages and (if any) prefixes to locate factory base classes.
 
Method Summary
 Leaf Char(String lit1)
          define undefined lit1.
static SubProcess createJay(boolean trace, boolean verbose, String prefix, File sourceFile, PrintWriter out, OutputStream err)
          create jay SubProcess.
static SubProcess createJLex(File sourceFile, boolean scannerStatic, PrintWriter out, OutputStream err, String javaLangSystem)
          create JLex SubProcess.
static PJ.Parser createParser(int[] nErrors, Reader reader, String name, String packages, String javaLangSystem)
          create parser tree from input file or standard input.
static void dumpParser(PrintWriter out, PJ.Parser parser)
          dump parser tree.
 Leaf Error()
          return errorInfo.
protected static String format(String key, Object[] args)
          format a string from the resource file.
 Leaf IdDef(String id, Leaf optInput)
          define undefined id as a token.
 Leaf IdRef(String id)
          within rules: define undefined id as unknown nonterminal.
static void insertFactory(PrintWriter out, File sourceFile, PJ.Parser parser, boolean flatten)
          merge generated factory into class frame from file.
 List Left(ArrayList operators)
          represent %left.
 Leaf Literal(String lit)
          define undefined lit.
 Leaf MacDef(String id, String lex)
          uniquely define regular expression for macro id.
 Leaf MacRef(String mac)
          reference a macro that will be defined later.
static void main(String[] args)
          process command line.
 Leaf Many(String id)
          define undefined id as unknown nonterminal.
 Leaf Name(String id)
          within %left, %right, or %nonassoc and as a delimeter: define undefined id as undefined, unrepresentable token.
 List Nonassoc(ArrayList operators)
          represent %nonassoc.
 Leaf Opt(String id)
          define undefined id as unknown nonterminal.
 List Parser(List optPackage, String optId, ArrayList decls, ArrayList lexs, String sep, ArrayList rules, ArrayList epilogs)
          top level.
protected static void print(PrintWriter out, String key, Object[] args)
          print a formatted string from the resource file.
 List Rhs(ArrayList items, Object optPrec, String optAction)
          arrange for an action call or for collection.
 List Right(ArrayList operators)
          represent %right.
 List Rule(String optType, String id, ArrayList rhss)
          uniquely define id as nonterminal, default type to java.lang.Object.
static int runJay(SubProcess jay)
          run jay.
static int runJLex(SubProcess jlex)
          run JLex.
 List Scan(ArrayList syms)
          represent %scan.
 List Screen(ArrayList macrefs)
          represent %screen.
 Leaf Sep(String id, Leaf operator)
          define undefined id as unknown nonterminal.
 List Skip(ArrayList macrefs)
          represent %skip.
 Leaf Some(String id)
          define undefined id as unknown nonterminal.
 Leaf Symbol(String sym)
          define undefined sym.
 List Token(String optType, ArrayList tokens)
          represent %token.
 String Type(ArrayList ids)
          return concatenated type path.
static void usage()
          describe usage and exit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

templates

public static final ResourceBundle templates
MessageFormat.format templates to generate output and other configuration parameters. pj.properties overwrites the filename stem.


rfc

protected final Rfc rfc
permits access to Rfc.yyerror(java.lang.String).


prefixes

protected final String[] prefixes
prefixes to try to locate a base class.

See Also:
PJ.Action.factory(java.io.PrintWriter, pj.PJ.IdInfo, boolean)

javaLangSystem

protected final String javaLangSystem
classname to use for System.


ids

protected final HashMap ids
maps ids to one-element arrays of PJ.IdInfo structures.

See Also:
Name(java.lang.String), IdDef(java.lang.String, pj.Leaf), IdRef(java.lang.String)

lits

protected final HashMap lits
maps lits to one-element arrays of PJ.LitInfo or PJ.Lit1Info structures.

See Also:
Char(java.lang.String), Literal(java.lang.String)

syms

protected final HashMap syms
maps syms to one-element arrays of PJ.SymInfo structures.

See Also:
Symbol(java.lang.String)

errorInfo

protected PJ.ErrorInfo errorInfo
singleton representing error.


uniq

protected int uniq
suffix for unique sym and lit names.


macs

protected final HashMap macs
maps macro names to one-element arrays of PJ.MacInfo structures.

See Also:
MacDef(java.lang.String, java.lang.String), MacRef(java.lang.String)

actions

protected final HashMap actions
maps action header to return type, avoids duplicate action signatures and inconsistent return types.

See Also:
PJ.Parser.factory(java.io.PrintWriter, boolean, boolean, boolean), PJ.Parser.prolog(java.io.PrintWriter), PJ.Action.factory(java.io.PrintWriter, pj.PJ.IdInfo, boolean), PJ.Action.prolog(java.io.PrintWriter, pj.PJ.IdInfo)

classes

protected final HashSet classes
avoids duplicate classes.

See Also:
PJ.Parser.factory(java.io.PrintWriter, boolean, boolean, boolean), PJ.Action.factory(java.io.PrintWriter, pj.PJ.IdInfo, boolean)

screened

protected boolean screened
true if %screen was specified.

See Also:
PJ.Screen

literalsUsed

protected boolean literalsUsed
true if long literals were used.

See Also:
Literal(java.lang.String)

skipped

protected boolean skipped
true if %skip was specified.


typed

protected boolean typed
true if type or action was specified.

Constructor Detail

PJ

public PJ(Rfc rfc,
          String prefixes,
          String javaLangSystem)
remember rfc for error messages and (if any) prefixes to locate factory base classes.

Parameters:
prefixes - may be null, defaulted from resource file.
javaLangSystem - used to reference System.
Method Detail

usage

public static void usage()
describe usage and exit.


format

protected static String format(String key,
                               Object[] args)
format a string from the resource file.

Parameters:
args - can be null if none.

print

protected static void print(PrintWriter out,
                            String key,
                            Object[] args)
print a formatted string from the resource file.

Parameters:
args - can be null if none.

Parser

public List Parser(List optPackage,
                   String optId,
                   ArrayList decls,
                   ArrayList lexs,
                   String sep,
                   ArrayList rules,
                   ArrayList epilogs)
top level.

Specified by:
Parser in interface Rfc.yyActions

Left

public List Left(ArrayList operators)
represent %left.

Specified by:
Left in interface Rfc.yyActions
See Also:
Name(java.lang.String), Char(java.lang.String), Literal(java.lang.String), Symbol(java.lang.String)

Nonassoc

public List Nonassoc(ArrayList operators)
represent %nonassoc.

Specified by:
Nonassoc in interface Rfc.yyActions
See Also:
Name(java.lang.String), Char(java.lang.String), Literal(java.lang.String), Symbol(java.lang.String)

Right

public List Right(ArrayList operators)
represent %right.

Specified by:
Right in interface Rfc.yyActions
See Also:
Name(java.lang.String), Char(java.lang.String), Literal(java.lang.String), Symbol(java.lang.String)

Scan

public List Scan(ArrayList syms)
represent %scan.

Specified by:
Scan in interface Rfc.yyActions

Screen

public List Screen(ArrayList macrefs)
represent %screen.

Specified by:
Screen in interface Rfc.yyActions

Skip

public List Skip(ArrayList macrefs)
represent %skip.

Specified by:
Skip in interface Rfc.yyActions

Token

public List Token(String optType,
                  ArrayList tokens)
represent %token.

Specified by:
Token in interface Rfc.yyActions

Name

public Leaf Name(String id)
within %left, %right, or %nonassoc and as a delimeter: define undefined id as undefined, unrepresentable token.

Specified by:
Name in interface Rfc.yyActions
Returns:
id as PJ.IdInfo.
See Also:
PJ.Sep

Char

public Leaf Char(String lit1)
define undefined lit1.

Specified by:
Char in interface Rfc.yyActions
Returns:
lit1 as a PJ.Lit1Info.

Literal

public Leaf Literal(String lit)
define undefined lit.

Specified by:
Literal in interface Rfc.yyActions
Returns:
lit as a PJ.LitInfo.

Symbol

public Leaf Symbol(String sym)
define undefined sym.

Specified by:
Symbol in interface Rfc.yyActions
Returns:
sym as a PJ.SymInfo.

MacRef

public Leaf MacRef(String mac)
reference a macro that will be defined later. This need not be unique because of screening.

Specified by:
MacRef in interface Rfc.yyActions
Returns:
mac as PJ.MacInfo.

Type

public String Type(ArrayList ids)
return concatenated type path.

Specified by:
Type in interface Rfc.yyActions

IdDef

public Leaf IdDef(String id,
                  Leaf optInput)
define undefined id as a token.

Specified by:
IdDef in interface Rfc.yyActions
Returns:
id as a PJ.IdInfo.

MacDef

public Leaf MacDef(String id,
                   String lex)
uniquely define regular expression for macro id. This is usually define after use.

Specified by:
MacDef in interface Rfc.yyActions
Returns:
PJ.MacInfo.

Rule

public List Rule(String optType,
                 String id,
                 ArrayList rhss)
uniquely define id as nonterminal, default type to java.lang.Object. Transform to represent id as PJ.IdInfo.

Specified by:
Rule in interface Rfc.yyActions
See Also:
IdRef(java.lang.String)

Rhs

public List Rhs(ArrayList items,
                Object optPrec,
                String optAction)
arrange for an action call or for collection.

Specified by:
Rhs in interface Rfc.yyActions
Returns:
PJ.Collect, or PJ.Action.

IdRef

public Leaf IdRef(String id)
within rules: define undefined id as unknown nonterminal.

Specified by:
IdRef in interface Rfc.yyActions
Returns:
id as PJ.IdInfo.

Error

public Leaf Error()
return errorInfo.

Specified by:
Error in interface Rfc.yyActions

Opt

public Leaf Opt(String id)
define undefined id as unknown nonterminal.

Specified by:
Opt in interface Rfc.yyActions
Returns:
id as a PJ.Opt to PJ.IdInfo marked to be used optionally.

Some

public Leaf Some(String id)
define undefined id as unknown nonterminal.

Specified by:
Some in interface Rfc.yyActions
Returns:
id as a PJ.Some to PJ.IdInfo marked to be used one or more times.

Many

public Leaf Many(String id)
define undefined id as unknown nonterminal.

Specified by:
Many in interface Rfc.yyActions
Returns:
id as a PJ.Many to PJ.IdInfo marked to be used zero or more times.

Sep

public Leaf Sep(String id,
                Leaf operator)
define undefined id as unknown nonterminal.

Specified by:
Sep in interface Rfc.yyActions
Returns:
id as a PJ.Sep to PJ.IdInfo marked to be used one or more times and separated by operator.

main

public static void main(String[] args)
process command line.

See Also:
usage()

createParser

public static PJ.Parser createParser(int[] nErrors,
                                     Reader reader,
                                     String name,
                                     String packages,
                                     String javaLangSystem)
                              throws IOException,
                                     Rfc.yyException
create parser tree from input file or standard input.

Parameters:
nErrors - first element counts error messages.
javaLangSystem - used to reference System.
Throws:
IOException
Rfc.yyException

dumpParser

public static void dumpParser(PrintWriter out,
                              PJ.Parser parser)
dump parser tree.


insertFactory

public static void insertFactory(PrintWriter out,
                                 File sourceFile,
                                 PJ.Parser parser,
                                 boolean flatten)
                          throws IOException
merge generated factory into class frame from file.

Parameters:
sourceFile - a line consisting only of %% is replaced by the tree factory methods and classes.
Throws:
IOException

createJLex

public static SubProcess createJLex(File sourceFile,
                                    boolean scannerStatic,
                                    PrintWriter out,
                                    OutputStream err,
                                    String javaLangSystem)
                             throws IOException
create JLex SubProcess.

Parameters:
out - receives generated Java source, not null.
javaLangSystem - replaces java.lang.System in JLex output. This is a kludge, but the JLex implementation does not encourage binary reuse.
Throws:
IOException

runJLex

public static int runJLex(SubProcess jlex)
                   throws IOException,
                          InterruptedException
run JLex.

Throws:
IOException
InterruptedException

createJay

public static SubProcess createJay(boolean trace,
                                   boolean verbose,
                                   String prefix,
                                   File sourceFile,
                                   PrintWriter out,
                                   OutputStream err)
                            throws IOException
create jay SubProcess.

Parameters:
prefix - can be null to use default y, only used if verbose is true.
sourceFile - can be null to use a temporary file.
Throws:
IOException

runJay

public static int runJay(SubProcess jay)
                  throws IOException,
                         InterruptedException
run jay.

Throws:
IOException
InterruptedException