Example #1
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 #2
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 #3
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 #4
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 #5
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 #6
0
 /** Formats an expression. */
 private void formatExpression() {
   if (!lexer.hasNext()) return;
   formatFactor();
   if (canFormatOperator()) {
     formatOperator();
     formatExpression();
   }
 }
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 the text input. */
  public NXTalkFormatterOutput formatInput(String input, int caretPosition) {
    // Reset the output data structures
    output.reset();
    // Set the output data structures and lex the input text
    output.set(input);
    lexer.lex(input);
    caret = caretPosition;

    // Go through each token in the input stream, and pass it to a
    // coloring function if it matches certain key phrases.
    while (lexer.hasNext()) {
      // Passes commands to a command formatter
      if (formatCommand()) {
      }
      // Passes loop commands to a loop formatter
      else if (formatLoopCommand()) {
      } else append(lexer.getNext(), Syntax.Null);
    }
    output.setCaretPosition(caret);
    return output;
  }
Example #9
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 #10
0
 /** Formats one or more motors. */
 private void formatMotors() {
   while (lexer.hasNext() && NXTalk.getMotor(lexer.peekNext()) != null)
     append(lexer.getNext(), Syntax.Motor);
 }
Example #11
0
 /** Formats a sensor. */
 private void formatSensorTerminal() {
   append(Syntax.Sensor);
   if (NXTalk.getSensorAction(lexer.peekNext()) != null) append(Syntax.SensorAction);
 }
Example #12
0
 /** Appends the given String text with the given syntax type. */
 private void append(String text, int syntaxType) {
   if (text == null || text.length() == 0) return;
   output.addFormatting(lexer.getRepresentation(text), "" + syntaxType);
 }
Example #13
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()));
 }
Example #14
0
 /** Appends the given text to the formatted output text. */
 private void append(String text) {
   output.addFormatting(lexer.getRepresentation(text), "" + lexer.getDictionary().contains(text));
 }
Example #15
0
 /**
  * Appends the next token in the lexer output to the NXTalkFormatterOutput instance, with the
  * given syntax type.
  */
 private void append(int syntaxType) {
   append(lexer.getNext(), syntaxType);
 }
Example #16
0
 /** Formats a motor direction statement. */
 private void formatDirection() {
   if (NXTalk.getDirection(lexer.peekNext()) != null) append(lexer.getNext(), Syntax.Direction);
 }
Example #17
0
 /** Formats a new line statement. */
 private void formatNewLine() {
   if (lexer.atCommandEnd()) append(lexer.getNext(), Syntax.Null);
 }
Example #18
0
 /** Formats an operator. */
 private void formatOperator() {
   append(lexer.getNext(), Syntax.Operator);
   if (lexer.atCommandEnd()) append();
 }
Example #19
0
 /** Prints a debug message. */
 private void debugPrint(String message) {
   System.out.println(message + "[" + lexer.peekNext() + "]");
 }
Example #20
0
 /** Returns true if the next token is an operator. */
 private boolean canFormatOperator() {
   return NXTalk.getOperator(lexer.peekNext()) != null;
 }
Example #21
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 #22
0
 /** Formats a unit. */
 private void formatUnit() {
   if (NXTalk.getUnit(lexer.peekNext()) != null) append(Syntax.Unit);
 }