trizehn

Parser

Nov 18th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. /**
  2.  * Write a description of class Parser here.
  3.  *
  4.  * @author Daffa Tristan Firdaus
  5.  * @version 18 November 2020
  6.  */
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;
  10. import java.util.StringTokenizer;
  11. import java.lang.String;
  12.  
  13. public class Parser
  14. {
  15.     private CommandWords commands;  // holds all valid command words
  16.  
  17.     public Parser()
  18.     {
  19.         commands = new CommandWords();
  20.     }
  21.  
  22.     public Command getCommand()
  23.     {
  24.         String inputLine = "";   // will hold the full input line
  25.         String word1;
  26.         String word2;
  27.            
  28.         System.out.print("> ");     // print prompt
  29.        
  30.         BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
  31.         try {
  32.             inputLine = reader.readLine();
  33.         }
  34.         catch (java.io.IOException exc){
  35.             System.out.println ("There was an error during reading: " + exc.getMessage());
  36.         }
  37.  
  38.         StringTokenizer tokenizer = new StringTokenizer(inputLine);
  39.  
  40.         if(tokenizer.hasMoreTokens())
  41.             word1 = tokenizer.nextToken();      // get the first word
  42.         else
  43.             word1=null;
  44.         if(tokenizer.hasMoreTokens())
  45.             word2 = tokenizer.nextToken();      // get the second word
  46.         else
  47.             word2=null;
  48.  
  49.         if(commands.isCommand(word1))
  50.             return new Command(word1, word2);
  51.         else
  52.             return new Command(null, word2);
  53.     }
  54. }
Add Comment
Please, Sign In to add comment