Example #1
0
 /** Formats a symbol declaration. */
 private void formatSymbolDeclaration() {
   if (lexer.hasNext() && lexer.peekNext().matches(NXTalk.validSymbolName)) {
     recognizedSymbols.add(lexer.peekNext());
     append(Syntax.Symbol);
   } else {
     append(Syntax.InvalidSymbol);
   }
 }
Example #2
0
 /** Returns true if this instance can format a scalar terminal. */
 private boolean canFormatScalarTerminal() {
   try {
     Integer.parseInt(lexer.peekNext());
     return true;
   } catch (NumberFormatException n) {
     try {
       Double.parseDouble(lexer.peekNext());
       return true;
     } catch (NumberFormatException n2) {
       return false;
     }
   }
 }
Example #3
0
 private void formatTerminal() {
   if (canFormatScalarTerminal()) {
     formatScalarTerminal();
     formatUnit();
   } else if (NXTalk.getSensor(lexer.peekNext()) != null) formatSensorTerminal();
   else if (lexer.peekNext().equals(NXTalk.stringDelimiter)) formatStringTerminal();
   else if (lexer.peekNext().equals("yes") || lexer.peekNext().equals("no"))
     append(lexer.getNext(), Syntax.Boolean);
   else if (recognizedSymbols.contains(lexer.peekNext())) {
     append(lexer.getNext(), Syntax.Symbol);
     formatUnit();
   }
 }
Example #4
0
 /**
  * Skips the next token if it matches the given input, and appends it to the output formatting
  * stream with the given syntax value.
  */
 private boolean skipNext(String token, int syntax) {
   if (lexer.hasNext() && lexer.peekNext().equals(token)) {
     append(lexer.getNext(), syntax);
     return true;
   }
   return false;
 }
Example #5
0
 /** Formats one or more motor directives that are appended to the end of motor commands. */
 private void formatMotorDirectives() {
   if (lexer.atCommandEnd()) return;
   while (NXTalk.getMotorDirective(lexer.peekNext()) != null) {
     append(lexer.getNext(), Syntax.MotorDirective);
     skipNext("and");
   }
 }
Example #6
0
 /**
  * Formats a loop command.
  *
  * @return
  */
 private boolean formatLoopCommand() {
   Loop l = NXTalk.getLoop(lexer.peekNext());
   if (l == null) return false;
   append(lexer.getNext(), Syntax.Loop);
   formatExpression();
   return true;
 }
Example #7
0
  /** Formats a string. */
  private void formatStringTerminal() {
    skipNext(NXTalk.stringDelimiter, Syntax.String);
    if (lexer.peekNext().indexOf(NXTalk.caretIndicator) != -1) {

    } else {
      append(Syntax.String);
    }
    skipNext(NXTalk.stringDelimiter, Syntax.String);
  }
Example #8
0
  /**
   * Formats subsets of the token stream that begin with an element of the Command enum of NXTalk.
   */
  private boolean formatCommand() {
    // Get the command, then append it to the output stream
    Command command = NXTalk.getCommand(lexer.peekNext());
    if (command == null) return false;
    append(Syntax.Command);

    // Pass evaluation of formatting to other formatting methods
    switch (command) {
      case Start:
        formatMotors(); // start <motor>+
        formatDirection(); // start <motor>+ <direction>?
        if (skipNext("and")) { // start <motor>+ <direction>? and? <inner motor command>
          formatMotors(); // <motor>+
          formatDirection(); // <direction>
        }
        if (skipNext("for")) // start <motor>+ <direction>? for <expr>
        formatExpression();
        formatMotorDirectives(); // ... <motor directive>*
        break;
      case Stop:
        formatMotors(); // stop <motor>+
        break;
      case Rotate:
        formatMotors(); // rotate <motor>+
        if (skipNext("to", Syntax.Command)) { // rotate <motor>+ to <expr>
          formatExpression();
        } else {
          formatDirection(); // rotate <motor>+ <direction>
          formatMotorDirectives(); // <motor directives>*
        }
      case Set:
        formatSymbolDeclaration();
        skipNext("to", Syntax.Command);
        formatExpression();
        break;
      case Wait:
        skipNext("until", Syntax.Command);
        formatExpression();
        break;
      case Coast:
        formatMotors();
        skipNext("until", Syntax.Keyword);
        skipNext("stop", Syntax.Keyword);
        break;
      case If:
        formatExpression(); // The branching condition
        indentLevel++;
        break;
      case ElseIf:
        formatExpression(); // The branching condition
        break;
      case Print:
        formatExpression(); // The expression being printed
        break;
      case EndCommand:
        /*if (indent) {
        	Command c = NXTalk.getCommand(lexer.peekNext());
        	if (c == Command.EndIf || c == Command.Else || c == Command.ElseIf ||
        		c == Command.EndRotate || c == Command.EndGroup || c == Command.EndRepeat)
        		break;
        	for (int i = 0 ; i < indentLevel ; i++)
        		append("\t", Syntax.Null);
        	if (caret > output.getFormattedLength())
        		caret++;
        }*/
        break;
    }
    return true;
  }
Example #9
0
 /** Prints a debug message. */
 private void debugPrint(String message) {
   System.out.println(message + "[" + lexer.peekNext() + "]");
 }
Example #10
0
 /** Formats a motor direction statement. */
 private void formatDirection() {
   if (NXTalk.getDirection(lexer.peekNext()) != null) append(lexer.getNext(), Syntax.Direction);
 }
Example #11
0
 /** Formats one or more motors. */
 private void formatMotors() {
   while (lexer.hasNext() && NXTalk.getMotor(lexer.peekNext()) != null)
     append(lexer.getNext(), Syntax.Motor);
 }
Example #12
0
 /** Formats a sensor. */
 private void formatSensorTerminal() {
   append(Syntax.Sensor);
   if (NXTalk.getSensorAction(lexer.peekNext()) != null) append(Syntax.SensorAction);
 }
Example #13
0
 /** Formats a unit. */
 private void formatUnit() {
   if (NXTalk.getUnit(lexer.peekNext()) != null) append(Syntax.Unit);
 }
Example #14
0
 /** Returns true if the next token is an operator. */
 private boolean canFormatOperator() {
   return NXTalk.getOperator(lexer.peekNext()) != null;
 }
Example #15
0
 /**
  * Appends the next token in the lexer output to the NXTalkFormatterOutput instance, inferring the
  * syntax type of the token.
  */
 private void append() {
   append(lexer.peekNext(), lexer.getDictionary().contains(lexer.getNext()));
 }