edu.princeton.toy
Class TProgramDocument

java.lang.Object
  extended by edu.princeton.swing.text.HighlightedDocument
      extended by edu.princeton.toy.TProgramDocument

public class TProgramDocument
extends HighlightedDocument

TProgramDocument is a class which encapsulates the information associated with a toy program. It handles the program text parsing and also interfaces with a TVirtualMachine to reset its memory to contain the program whenever the workspace's reset() function is called.


Nested Class Summary
static class TProgramDocument.WarningInfoStruct
          WarningInfoStruct is a simple class which encapsulates all of the info that getWarnings() has to return.
 
Nested classes/interfaces inherited from class edu.princeton.swing.text.HighlightedDocument
HighlightedDocument.Position, HighlightedDocument.PositionTriplet, HighlightedDocument.UndoableEdit
 
Field Summary
static int COMMENT_COLUMN
          The column at which normal (non-autocomment) comments begin.
static String DEFAULT_TITLE
          The default title a workspace should take if no program line was found.
static String HEADER_BAR
          The bar the separates the description of a program from the code of the program.
static int MAX_WARNINGS
          The maximum number of warnings before a fatal error is thrown.
static int OUTSIDE_MARGIN_COLUMN
          The column at which text is considered past the margin.
static byte STYLE_AUTO_COMMENT
          The index of the auto-comment style.
static byte STYLE_COMMENT
          The index of the comment style.
static byte STYLE_IDENTIFIER
          The index of the header style.
static byte STYLE_INSTRUCTION
          The index of the instruction style.
static byte STYLE_KEYWORD
          The index of the keyword style.
static byte STYLE_OUTSIDE_MARGIN
          The index of the outside-margin style.
 
Fields inherited from class edu.princeton.swing.text.HighlightedDocument
CHARACTER_ALLOWED, charCount, chars, charStyles, lineCount, lineOffsets, MAX_ALLOWED_CHARACTER, maxLineLength
 
Constructor Summary
TProgramDocument()
          Constructs a TProgramDocument with a template program.
TProgramDocument(String text)
          Constructs a TProgramDocument with the given program.
 
Method Summary
 void assignStyles()
          Parses toy code and uses it to highlight the syntax, update the title, and and update the warning list.
 void autocomment()
          Updates pseudocode comments and performs minor stylistic changes (standardizing capitalization and putting '//'s before all comments).
 TWordBuffer getInitialStdin()
          Returns the initial values which should be in stdin whenever the workspace is reset.
 TWordBuffer getInitialStdin(TWordBuffer buffer)
          Returns the initial values which should be in stdin whenever the workspace is reset.
 int getLineDefined(int address, boolean returnLastLine)
          Returns the line on which the given memory address was defined.
 int getStyleCount()
          Returns the number of distinct styles used by this type of document.
 int getTabSize()
          Returns the number of spaces to replace all tabs with.
 String getTitle()
          Returns the title of the workspace.
 TProgramDocument.WarningInfoStruct getWarnings(boolean ignoreWarnings)
          Returns a list of warnings along with character indices of the start and end of the relevant text.
 TProgramDocument.WarningInfoStruct getWarnings(TProgramDocument.WarningInfoStruct warningInfo, boolean ignoreWarnings)
          Returns a list of warnings along with character indices of the start and end of the relevant text.
 void reset(TVirtualMachine virtualMachine)
          Resets the virtual machine then copies the program and initial stdin back in.
 void setInitialStdin(TWordBuffer initialStdin)
          Sets the initial values which should be in stdin whenever the workspace is reset.
 void stripComments()
          Strips all comments (except header comments) off the program.
 
Methods inherited from class edu.princeton.swing.text.HighlightedDocument
addTextListener, addUndoableEditListener, allowRender, coordinateToOffset, coordinateToOffset, createPosition, createPositionTriplet, filterString, freePosition, freePositionTriplet, getLength, getLineCount, getMaxLineLength, getPositionOffsets, getText, getText, getWordBounds, insertString, offsetToCoordinate, readLock, readUnlock, remove, removeTextListener, removeUndoableEditListener, replace, replace, setText, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_WARNINGS

public static final int MAX_WARNINGS
The maximum number of warnings before a fatal error is thrown.

See Also:
Constant Field Values

STYLE_IDENTIFIER

public static final byte STYLE_IDENTIFIER
The index of the header style.

See Also:
Constant Field Values

STYLE_INSTRUCTION

public static final byte STYLE_INSTRUCTION
The index of the instruction style.

See Also:
Constant Field Values

STYLE_KEYWORD

public static final byte STYLE_KEYWORD
The index of the keyword style.

See Also:
Constant Field Values

STYLE_COMMENT

public static final byte STYLE_COMMENT
The index of the comment style.

See Also:
Constant Field Values

STYLE_AUTO_COMMENT

public static final byte STYLE_AUTO_COMMENT
The index of the auto-comment style.

See Also:
Constant Field Values

STYLE_OUTSIDE_MARGIN

public static final byte STYLE_OUTSIDE_MARGIN
The index of the outside-margin style.

See Also:
Constant Field Values

DEFAULT_TITLE

public static final String DEFAULT_TITLE
The default title a workspace should take if no program line was found.

See Also:
Constant Field Values

HEADER_BAR

public static final String HEADER_BAR
The bar the separates the description of a program from the code of the program.

See Also:
Constant Field Values

COMMENT_COLUMN

public static final int COMMENT_COLUMN
The column at which normal (non-autocomment) comments begin.

See Also:
Constant Field Values

OUTSIDE_MARGIN_COLUMN

public static final int OUTSIDE_MARGIN_COLUMN
The column at which text is considered past the margin.

See Also:
Constant Field Values
Constructor Detail

TProgramDocument

public TProgramDocument()
Constructs a TProgramDocument with a template program.


TProgramDocument

public TProgramDocument(String text)
Constructs a TProgramDocument with the given program.

Method Detail

getLineDefined

public int getLineDefined(int address,
                          boolean returnLastLine)
Returns the line on which the given memory address was defined.

Parameters:
address - The address in question. An invalid value will result in an ArrayIndexOutOfBounds exception.
returnLastLine - Wheter or not to return the last line if the address was never defined.
Returns:
The line on which the given memory address was defined. Either the last line or -1 is returned if the given memory address was never defined (depending on the value of returnLastLine).

getWarnings

public TProgramDocument.WarningInfoStruct getWarnings(boolean ignoreWarnings)
Returns a list of warnings along with character indices of the start and end of the relevant text.

Parameters:
ignoreWarnings - Wheter or not to ignore normal warnings and only report fatal errors.
Returns:
A newly allocated WarningInfoStruct containing a list of warnings along with character indices of the start and end of the relevant text.

getWarnings

public TProgramDocument.WarningInfoStruct getWarnings(TProgramDocument.WarningInfoStruct warningInfo,
                                                      boolean ignoreWarnings)
Returns a list of warnings along with character indices of the start and end of the relevant text.

Parameters:
warningInfo - A WarningInfoStruct which can be reused. If this is null, a new WarningInfoStruct will be returned.
ignoreWarnings - Wheter or not to ignore normal warnings and only report fatal errors.
Returns:
A newly allocated (or newly modified) WarningInfoStruct containing a list of warnings along with character indices of the start and end of the relevant text.

getInitialStdin

public TWordBuffer getInitialStdin()
Returns the initial values which should be in stdin whenever the workspace is reset. A clone of the internal buffer is returned.

Returns:
A TWordBuffer containing the initial values which should be in stdin whenever the workspace is reset.

getInitialStdin

public TWordBuffer getInitialStdin(TWordBuffer buffer)
Returns the initial values which should be in stdin whenever the workspace is reset. The internal buffer's data is copied into the provided buffer.

Parameters:
buffer - The buffer to copy the data onto. The buffer will be cleared before the copy.
Returns:
The same buffer which was passed in.

setInitialStdin

public void setInitialStdin(TWordBuffer initialStdin)
Sets the initial values which should be in stdin whenever the workspace is reset.

Parameters:
initialStdin - The new values which should be in stdin whenever the workspace is reset.

getTitle

public String getTitle()
Returns the title of the workspace. This is derived from the word(s) following "Program " in the first line of the program.

Returns:
The title of the workspace.

reset

public void reset(TVirtualMachine virtualMachine)
Resets the virtual machine then copies the program and initial stdin back in.


getStyleCount

public int getStyleCount()
Returns the number of distinct styles used by this type of document. This must always return the same number.

Specified by:
getStyleCount in class HighlightedDocument
Returns:
The number of distinct styles used by this type of document.

getTabSize

public int getTabSize()
Returns the number of spaces to replace all tabs with.

Specified by:
getTabSize in class HighlightedDocument
Returns:
The number of spaces to replace all tabs with.

assignStyles

public void assignStyles()
Parses toy code and uses it to highlight the syntax, update the title, and and update the warning list. Calls to getWarnings(), getProgramSummary(), and reset() should be made as necessary to make sure that the changes propagate to the warning list, program summary, and the virtual machine.

Specified by:
assignStyles in class HighlightedDocument

autocomment

public void autocomment()
Updates pseudocode comments and performs minor stylistic changes (standardizing capitalization and putting '//'s before all comments).


stripComments

public void stripComments()
Strips all comments (except header comments) off the program.