home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / io / storyparser.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  15.1 KB  |  506 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.  
  15.     @(#)StoryParser.java    0.00 20-Dec-95
  16.  
  17.         A parser that reads from an InputStream
  18.         and writes to a StoryContainer.
  19.  
  20.         This code is based on StdoutParser.java, which in turn was
  21.         generated from StdoutParser.cup by JavaCup v0.9b
  22.         Tue Dec 26 13:17:16  1995
  23.  
  24.     Authors:
  25.  
  26.         rphall      Rick Hall
  27.  
  28.  
  29.     Version Ident:
  30.  
  31.         $Header$
  32.  
  33.  
  34.     History:
  35.  
  36.         12/20/95    rphall      Initial Creation
  37.  
  38. ---------------------------------------------------------------------------*/
  39.  
  40. package pj.io;
  41.  
  42. import pj.io.IncompatiblePaperStoryException;
  43. import pj.io.MalformedPaperStoryException;
  44. import pj.io.Paper;
  45. import pj.io.PaperStory;
  46. import pj.io.StoryContainer;
  47. import pj.io.StoryScanner;
  48.  
  49. import collections.ImplementationError;
  50. import java.io.InputStream;
  51. import java.lang.IllegalArgumentException;
  52. import java_cup.runtime.*;
  53.  
  54. public class StoryParser extends java_cup.runtime.lr_parser
  55.     {
  56.  
  57.     // --- Instance variables
  58.  
  59.     InputStream     is;
  60.     StoryContainer  sc;
  61.     StoryScanner    scanner;
  62.     PaperStory      currentStory;
  63.  
  64.     // --- Instance of action encapsulation class
  65.      protected CUP$actions action_obj;
  66.  
  67.     /** production table */
  68.     protected static final short _production_table[][] =
  69.         {
  70.         {5, 2},     {0, 2},     {5, 1},     {9, 0},     {4, 4},
  71.         {3, 1},     {3, 2},     {10, 0},     {2, 3},     {1, 2},
  72.         {1, 2},     {1, 2},     {1, 2},     {1, 1},     {1, 1},
  73.         {1, 1},     {1, 1},     {7, 3},     {6, 3},     {8, 2},
  74.         {8, 1}
  75.         };
  76.  
  77.  
  78.     /** parse action table */
  79.     protected static final short[][] _action_table =
  80.         {
  81.         /*0*/{2,3,-1,0},
  82.         /*1*/{0,29,2,3,-1,0},
  83.         /*2*/{8,-4,-1,0},
  84.         /*3*/{0,-3,2,-3,-1,0},
  85.         /*4*/{8,6,-1,0},
  86.         /*5*/{9,-8,-1,0},
  87.         /*6*/{3,11,4,10,5,-6,10,16,11,15,-1,0},
  88.         /*7*/{5,9,-1,0},
  89.         /*8*/{0,-5,2,-5,-1,0},
  90.         /*9*/{12,22,-1,0},
  91.         /*10*/{12,22,-1,0},
  92.         /*11*/{3,-17,4,-17,5,-17,10,-17,11,-17,-1,0},
  93.         /*12*/{3,11,4,10,5,-7,10,20,11,19,-1,0},
  94.         /*13*/{3,-16,4,-16,5,-16,10,-16,11,-16,-1,0},
  95.         /*14*/{3,-15,4,-15,5,-15,10,-15,11,-15,-1,0},
  96.         /*15*/{3,-14,4,-14,5,-14,10,-14,11,-14,-1,0},
  97.         /*16*/{3,-13,4,-13,5,-13,10,-13,11,-13,-1,0},
  98.         /*17*/{3,-12,4,-12,5,-12,10,-12,11,-12,-1,0},
  99.         /*18*/{3,-11,4,-11,5,-11,10,-11,11,-11,-1,0},
  100.         /*19*/{3,-10,4,-10,5,-10,10,-10,11,-10,-1,0},
  101.         /*20*/{6,23,12,24,-1,0},
  102.         /*21*/{6,-21,7,-21,12,-21,-1,0},
  103.         /*22*/{3,-18,4,-18,5,-18,10,-18,11,-18,-1,0},
  104.         /*23*/{6,-20,7,-20,12,-20,-1,0},
  105.         /*24*/{7,26,12,24,-1,0},
  106.         /*25*/{3,-19,4,-19,5,-19,10,-19,11,-19,-1,0},
  107.         /*26*/{9,28,-1,0},
  108.         /*27*/{3,-9,4,-9,5,-9,10,-9,11,-9,-1,0},
  109.         /*28*/{0,-2,-1,0},
  110.         /*29*/{0,-1,2,-1,-1,0},
  111.         };
  112.  
  113.     /** reduce_goto table */
  114.     protected static final short[][] _reduce_table =
  115.         {
  116.         /*0*/{4,3,5,1,-1,-1},
  117.         /*1*/{4,29,-1,-1},
  118.         /*2*/{9,4,-1,-1},
  119.         /*3*/{-1,-1},
  120.         /*4*/{2,6,3,7,-1,-1},
  121.         /*5*/{10,26,-1,-1},
  122.         /*6*/{1,12,6,11,7,13,-1,-1},
  123.         /*7*/{-1,-1},
  124.         /*8*/{-1,-1},
  125.         /*9*/{8,24,-1,-1},
  126.         /*10*/{8,20,-1,-1},
  127.         /*11*/{-1,-1},
  128.         /*12*/{6,16,7,17,-1,-1},
  129.         /*13*/{-1,-1},
  130.         /*14*/{-1,-1},
  131.         /*15*/{-1,-1},
  132.         /*16*/{-1,-1},
  133.         /*17*/{-1,-1},
  134.         /*18*/{-1,-1},
  135.         /*19*/{-1,-1},
  136.         /*20*/{-1,-1},
  137.         /*21*/{-1,-1},
  138.         /*22*/{-1,-1},
  139.         /*23*/{-1,-1},
  140.         /*24*/{-1,-1},
  141.         /*25*/{-1,-1},
  142.         /*26*/{-1,-1},
  143.         /*27*/{-1,-1},
  144.         /*28*/{-1,-1},
  145.         /*29*/{-1,-1},
  146.         };
  147.  
  148.     // --- Public constructors
  149.  
  150.     //public StoryParser() {super();}
  151.  
  152.     public StoryParser(InputStream input, StoryContainer output)
  153.         {
  154.         is = input;
  155.         sc = output;
  156.         scanner = new StoryScanner(is);
  157.         currentStory = null;
  158.         System.out.println("Debug-StoryParser:constructed");
  159.         }
  160.  
  161.     public StoryParser(StoryContainer output)
  162.         {
  163.         this(System.in,output);
  164.         }
  165.  
  166.     // --- Public operations
  167.  
  168.     /** access to production table */
  169.     public short[][] production_table()
  170.         {
  171.         return _production_table;
  172.         }
  173.  
  174.  
  175.     /** access to parse action table */
  176.     public short[][] action_table()
  177.         {
  178.         return _action_table;
  179.         }
  180.  
  181.  
  182.     /** access to reduce_goto table */
  183.     public short[][] reduce_table()
  184.         {
  185.         return _reduce_table;
  186.         }
  187.  
  188.  
  189.     /** action encapsulation object initializer */
  190.     protected void init_actions()
  191.         {
  192.         action_obj = new CUP$actions(this);
  193.         }
  194.  
  195.     /** invoke a user supplied parse action */
  196.     public java_cup.runtime.symbol do_action(
  197.                 int                        act_num,
  198.                 java_cup.runtime.lr_parser parser,
  199.                 java.util.Stack            stack,
  200.                 int                        top)
  201.         throws java.lang.Exception
  202.         {
  203.         /* call code in generated class */
  204.         return action_obj.CUP$do_action(act_num, parser, stack, top);
  205.         }
  206.  
  207.     /** start state */
  208.     public int start_state()
  209.         {
  210.         return 0;
  211.         }
  212.  
  213.     /** start production */
  214.     public int start_production()
  215.         {
  216.         return 1;
  217.         }
  218.  
  219.     /** EOF symbol index */
  220.     public int EOF_sym()
  221.         {
  222.         return 0;
  223.         }
  224.  
  225.     /** error symbol index */
  226.     public int error_sym()
  227.         {
  228.         return 1;
  229.         }
  230.  
  231.  
  232.     /** user initialization */
  233.     public void user_init() throws java.lang.Exception
  234.         {
  235.         scanner.init();
  236.         }
  237.  
  238.     /** scan to get the next token */
  239.     public java_cup.runtime.token scan() throws java.lang.Exception
  240.         {
  241.         return scanner.next_token();
  242.         }
  243.     }; //StoryParser
  244.  
  245.  
  246. /** JavaCup generated class to encapsulate user supplied action code.*/
  247. class CUP$actions
  248.     {
  249.  
  250.     // --- Instance variables
  251.     private StoryParser parser;
  252.  
  253.     /** Constructor */
  254.     CUP$actions(StoryParser sp)
  255.         {
  256.         parser = sp;
  257.         }
  258.  
  259.  
  260.  
  261.     /** Method with the actual generated action code. */
  262.     public final java_cup.runtime.symbol CUP$do_action(
  263.             int                        CUP$act_num,
  264.             java_cup.runtime.lr_parser CUP$parser,
  265.             java.util.Stack            CUP$stack,
  266.             int                        CUP$top)
  267.         throws java.lang.Exception
  268.         {
  269.         /* object for return from actions */
  270.         java_cup.runtime.symbol CUP$result;
  271.  
  272.         /* select the action based on the action number */
  273.         switch (CUP$act_num)
  274.             {
  275.             /*. . . . . . . . . . . . . . . . . . . .*/
  276.             case 20: // lines ::= LINE
  277.                 {
  278.                 CUP$result = new str_token(/*lines*/8);
  279.  
  280.                 ((str_token)CUP$result).str_val = (/*l*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val;
  281.  
  282.                 }
  283.             return CUP$result;
  284.  
  285.             /*. . . . . . . . . . . . . . . . . . . .*/
  286.             case 19: // lines ::= lines LINE
  287.                 {
  288.                 CUP$result = new str_token(/*lines*/8);
  289.  
  290.                 ((str_token)CUP$result).str_val = (/*ll*/(str_token)CUP$stack.elementAt(CUP$top-1)).str_val + (/*l*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val;
  291.  
  292.                 }
  293.             return CUP$result;
  294.  
  295.             /*. . . . . . . . . . . . . . . . . . . .*/
  296.             case 18: // attrib_body ::= START_BODY lines STOP_BODY
  297.                 {
  298.                 CUP$result = new str_token(/*attrib_body*/6);
  299.  
  300.                 ((str_token)CUP$result).str_val = (/*body*/(str_token)CUP$stack.elementAt(CUP$top-1)).str_val;
  301.  
  302.                 }
  303.             return CUP$result;
  304.  
  305.             /*. . . . . . . . . . . . . . . . . . . .*/
  306.             case 17: // attrib_title ::= START_TITLE lines STOP_TITLE
  307.                 {
  308.                 CUP$result = new str_token(/*attrib_title*/7);
  309.  
  310.                 ((str_token)CUP$result).str_val = (/*title*/(str_token)CUP$stack.elementAt(CUP$top-1)).str_val;
  311.  
  312.                 }
  313.             return CUP$result;
  314.  
  315.             /*. . . . . . . . . . . . . . . . . . . .*/
  316.             case 16: // attribs_opt ::= attrib_body
  317.                 {
  318.                 CUP$result = new symbol(/*attribs_opt*/1);
  319.  
  320.                 parser.currentStory.setBody((/*body*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  321.  
  322.                 }
  323.             return CUP$result;
  324.  
  325.             /*. . . . . . . . . . . . . . . . . . . .*/
  326.             case 15: // attribs_opt ::= attrib_title
  327.                 {
  328.                 CUP$result = new symbol(/*attribs_opt*/1);
  329.  
  330.                 parser.currentStory.setTitle((/*title*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  331.  
  332.                 }
  333.             return CUP$result;
  334.  
  335.             /*. . . . . . . . . . . . . . . . . . . .*/
  336.             case 14: // attribs_opt ::= ATTRIB_KEYS
  337.                 {
  338.                 CUP$result = new symbol(/*attribs_opt*/1);
  339.  
  340.                 parser.currentStory.setKeys((/*keys*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  341.  
  342.                 }
  343.             return CUP$result;
  344.  
  345.             /*. . . . . . . . . . . . . . . . . . . .*/
  346.             case 13: // attribs_opt ::= ATTRIB_TYPE
  347.                 {
  348.                 CUP$result = new symbol(/*attribs_opt*/1);
  349.  
  350.                 parser.currentStory.setType((/*type*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  351.  
  352.                 }
  353.             return CUP$result;
  354.  
  355.             /*. . . . . . . . . . . . . . . . . . . .*/
  356.             case 12: // attribs_opt ::= attribs_opt attrib_body
  357.                 {
  358.                 CUP$result = new symbol(/*attribs_opt*/1);
  359.  
  360.                 parser.currentStory.setBody((/*body*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  361.  
  362.                 }
  363.             return CUP$result;
  364.  
  365.             /*. . . . . . . . . . . . . . . . . . . .*/
  366.             case 11: // attribs_opt ::= attribs_opt attrib_title
  367.                 {
  368.                 CUP$result = new symbol(/*attribs_opt*/1);
  369.  
  370.                 parser.currentStory.setTitle((/*title*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  371.  
  372.                 }
  373.             return CUP$result;
  374.  
  375.             /*. . . . . . . . . . . . . . . . . . . .*/
  376.             case 10: // attribs_opt ::= attribs_opt ATTRIB_KEYS
  377.                 {
  378.                 CUP$result = new symbol(/*attribs_opt*/1);
  379.  
  380.                 parser.currentStory.setKeys((/*keys*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  381.  
  382.                 }
  383.             return CUP$result;
  384.  
  385.             /*. . . . . . . . . . . . . . . . . . . .*/
  386.             case 9: // attribs_opt ::= attribs_opt ATTRIB_TYPE
  387.                 {
  388.                 CUP$result = new symbol(/*attribs_opt*/1);
  389.  
  390.                 parser.currentStory.setType((/*type*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  391.  
  392.                 }
  393.             return CUP$result;
  394.  
  395.             /*. . . . . . . . . . . . . . . . . . . .*/
  396.             case 8: // attribs_reqd ::= ATTRIB_SECTION NT$1 ATTRIB_SUBSECT
  397.                 {
  398.                 CUP$result = new symbol(/*attribs_reqd*/2);
  399.  
  400.                 parser.currentStory.setSubsect((/*sub*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  401.  
  402.                 }
  403.             return CUP$result;
  404.  
  405.             /*. . . . . . . . . . . . . . . . . . . .*/
  406.             case 7: // NT$1 ::=
  407.                 {
  408.                 CUP$result = new java_cup.runtime.token(/*NT$1*/10);
  409.  
  410.                 parser.currentStory.setSection((/*sec*/(str_token)CUP$stack.elementAt(CUP$top-0)).str_val);
  411.  
  412.                 }
  413.             return CUP$result;
  414.  
  415.             /*. . . . . . . . . . . . . . . . . . . .*/
  416.             case 6: // data_story ::= attribs_reqd attribs_opt
  417.                 {
  418.                 CUP$result = new symbol(/*data_story*/3);
  419.  
  420.                 }
  421.             return CUP$result;
  422.  
  423.             /*. . . . . . . . . . . . . . . . . . . .*/
  424.             case 5: // data_story ::= attribs_reqd
  425.                 {
  426.                 CUP$result = new symbol(/*data_story*/3);
  427.  
  428.                 }
  429.             return CUP$result;
  430.  
  431.             /*. . . . . . . . . . . . . . . . . . . .*/
  432.             case 4: // story ::= START_STORY NT$0 data_story STOP_STORY
  433.                 {
  434.                 CUP$result = new symbol(/*story*/4);
  435.                 }
  436.             return CUP$result;
  437.  
  438.             /*. . . . . . . . . . . . . . . . . . . .*/
  439.             case 3: // NT$0 ::=
  440.             {
  441.                 CUP$result = new java_cup.runtime.token(/*NT$0*/9);
  442.  
  443.                 parser.currentStory = new PaperStory();
  444.  
  445.                 }
  446.             return CUP$result;
  447.  
  448.             /*. . . . . . . . . . . . . . . . . . . .*/
  449.             case 2: // story_list ::= story
  450.                 {
  451.                 CUP$result = new symbol(/*story_list*/5);
  452.  
  453.                 addStoryToContainer(parser.currentStory,parser.sc);
  454.                 parser.currentStory = null;
  455.  
  456.                 }
  457.             return CUP$result;
  458.  
  459.             /*. . . . . . . . . . . . . . . . . . . .*/
  460.             case 1: // $START ::= story_list EOF
  461.                 {
  462.                 CUP$result = new java_cup.runtime.token(/*$START*/0);
  463.  
  464.                 }
  465.             /* ACCEPT */
  466.             CUP$parser.done_parsing();
  467.             return CUP$result;
  468.  
  469.             /*. . . . . . . . . . . . . . . . . . . .*/
  470.             case 0: // story_list ::= story_list story
  471.                 {
  472.                 CUP$result = new symbol(/*story_list*/5);
  473.  
  474.                 addStoryToContainer(parser.currentStory,parser.sc);
  475.                 parser.currentStory = null;
  476.  
  477.                 }
  478.             return CUP$result;
  479.  
  480.             /* . . . . . .*/
  481.             default:
  482.             throw new Exception(
  483.                "Invalid action number found in internal parse table");
  484.  
  485.             } //switch
  486.         } //java_cup.runtime.symbol CUP$do_action
  487.  
  488.     // --- Private operations
  489.  
  490.     // Private method to add parser.currentStory to StoryContainer
  491.     private void addStoryToContainer(PaperStory ps, StoryContainer c)
  492.         {
  493.         try {
  494.             ps.checkImplementation();
  495.             c.addStory(ps);
  496.             }
  497.  
  498.         catch( ImplementationError e)             {}
  499.         catch( MalformedPaperStoryException e)    {}
  500.         catch( IncompatiblePaperStoryException e) {}
  501.         catch( IllegalArgumentException e)        {}
  502.  
  503.         }
  504. };//class CUP$actions
  505.  
  506.