public class CodeStream
extends java.lang.Object
_BLOCKSTART CodeBlockName [(Parameter1, Parameter2), ...] _HEADEREND CodeBlockBody _BLOCKENDParameterized code blocks can contain parameters which the user can specify. Parameter substitution syntax is straight-forward string pattern substitution, so the user is responsible for declaring unique parameter names. For example, a code block is declared to be the following: FIXME: $ref()'s should be replaced with $get() and $put().
/*** initBlock ($arg) ***/ if ($get(input) != $arg) { $put(output, $arg); } /**/If the user invoke the appendCodeBlock() method with a single argument, which is the integer 3,
LinkedList args = new LinkedList(); args.add(Integer.toString(3)); appendCodeBlock("initBlock", args);then after parameter substitution, the code block would become: FIXME: $ref()'s should be replaced with $get() and $put().
if ($get(input) != 3) { $put(output, 3); }Parameter substitution takes place before macro substitution processed by the codegen kernel. CodeStream supports overriding superclass code blocks. It also supports overloading code blocks with different number of parameters.
Modifier and Type | Class and Description |
---|---|
static class |
CodeStream.Signature
Inner class for representing a code block signature.
|
Modifier and Type | Field and Description |
---|---|
protected ProgramCodeGenerator |
_codeGenerator
The code generator associated with this code stream.
|
Constructor and Description |
---|
CodeStream(java.util.List<java.lang.String> templateArguments,
ProgramCodeGeneratorAdapter adapter)
Construct a new code stream associated with the given java actor
adapter.
|
CodeStream(ProgramCodeGeneratorAdapter adapter)
Construct a new code stream associated with the given java actor
adapter.
|
CodeStream(java.lang.String path,
ProgramCodeGenerator generator)
Construct a new code stream, given a specified file path of the
adapter .
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.lang.String codeBlock)
Append the contents of the given String to this code stream.
|
void |
appendCodeBlock(java.lang.String blockName)
Append the code block specified the given block name.
|
void |
appendCodeBlock(java.lang.String blockName,
boolean mayNotExist)
Append the code block specified the given block name.
|
void |
appendCodeBlock(java.lang.String blockName,
boolean mayNotExist,
int indentLevel)
Append the code block specified the given block name.
|
void |
appendCodeBlock(java.lang.String blockName,
java.util.List<java.lang.String> arguments)
Append the specific code block with an array of arguments and
substitute each argument with the parameters of the code block in
the order listed in the given arguments array list.
|
void |
appendCodeBlock(java.lang.String blockName,
java.util.List<java.lang.String> arguments,
boolean mayNotExist)
Append the specific code block with an array of arguments and
substitute each argument with the parameters of the code block in
the order listed in the given arguments array list.
|
void |
appendCodeBlock(java.lang.String blockName,
java.util.List<java.lang.String> arguments,
boolean mayNotExist,
int indentLevel)
Append the specific code block with an array of arguments and
substitute each argument with the parameters of the code block in
the order listed in the given arguments array list.
|
void |
appendCodeBlock(java.lang.String blockName,
java.util.List<java.lang.String> arguments,
int indentLevel)
Append the specific code block with an array of arguments and
substitute each argument with the parameters of the code block in
the order listed in the given arguments array list.
|
void |
appendCodeBlocks(java.lang.String nameExpression)
Append multiple code blocks whose names match the given regular
expression.
|
void |
clear()
Clear the contents of this code stream.
|
java.lang.String |
description()
Return a String that contains all the code block names and
bodies from the associated adapter .
|
java.util.List<java.lang.String> |
getAllCodeBlockNames()
Return a list of code block names contained by this CodeStream.
|
java.util.Set<CodeStream.Signature> |
getAllCodeBlockSignatures()
Return a set of code block signatures contained by this CodeStream.
|
java.lang.String |
getCodeBlock(java.lang.String name)
Given a code block name, return the corresponding code block.
|
java.lang.String |
getCodeBlock(java.lang.String blockName,
java.util.List<java.lang.String> arguments)
Return a codeBlock with a given name and substitute in the
given arguments.
|
java.lang.String |
getCodeBlock(java.lang.String blockName,
java.util.List<java.lang.String> arguments,
boolean mayNotExist)
Return a codeBlock with a given name and substitute in the
given arguments.
|
java.lang.String |
getCodeBlockTemplate(java.lang.Object signature)
Given a code block signature, return the corresponding code block
template.
|
static java.lang.String |
indent(int indentLevel,
java.lang.String inputString)
Indent the string to the specified indent level.
|
static java.lang.String |
indent(java.lang.String inputString)
Indent the string to the default indent level.
|
void |
insert(int offset,
java.lang.String code)
Insert the contents of the given String to this code stream
at the given position.
|
boolean |
isEmpty()
return a boolean indicating if this stream is empty.
|
static void |
main(java.lang.String[] args)
Simple stand alone test method.
|
void |
parse(java.lang.String filePath)
Parse additional code blocks from the file specified by the given
file path.
|
void |
parse(java.lang.String filePath,
boolean mayNotExist)
Parse additional code blocks from the file specified by the given
file path.
|
void |
reset()
Reset this CodeStream object so that its code table will be
re-constructed when needed.
|
static void |
selfTest()
Perform a self test.
|
void |
setCodeBlocks(java.lang.String codeBlocks)
Set the code blocks which will be parsed instead of .c file.
|
static void |
setIndentLevel(int indentLevel)
Set the indent level.
|
java.lang.String |
toString()
Return the string representation of the code stream.
|
protected ProgramCodeGenerator _codeGenerator
public CodeStream(ProgramCodeGeneratorAdapter adapter)
adapter
- The actor adapter associated with this code stream,
which is currently ignored.public CodeStream(java.util.List<java.lang.String> templateArguments, ProgramCodeGeneratorAdapter adapter)
templateArguments
- Template arguments to be substituted
in the code. Template arguments begin with "<" and end with ">".adapter
- The actor adapter associated with this code stream,
which is currently ignored.public CodeStream(java.lang.String path, ProgramCodeGenerator generator)
FileUtilities.openForReading(String, URI, ClassLoader)
,
for example "file:./test/testCodeBlock.c".path
- The given file path.generator
- The generator associated with this CodeStream.public void append(java.lang.String codeBlock)
codeBlock
- The given string.public void appendCodeBlock(java.lang.String blockName) throws IllegalActionException
blockName
- The given code block name.IllegalActionException
- If
appendCodeBlock(String, List, boolean) throws the exception.appendCodeBlock(String, List, boolean)
public void appendCodeBlock(java.lang.String blockName, boolean mayNotExist) throws IllegalActionException
blockName
- The given code block name.mayNotExist
- Indicate if it is okay not to find the code block.
if the code block has parameters.IllegalActionException
- If
appendCodeBlock(String, List, boolean) throws the exception.appendCodeBlock(String, List, boolean)
public void appendCodeBlock(java.lang.String blockName, boolean mayNotExist, int indentLevel) throws IllegalActionException
blockName
- The given code block name.mayNotExist
- Indicate if it is okay not to find the code block.
if the code block has parameters.indentLevel
- The level of indention.IllegalActionException
- If
appendCodeBlock(String, List, boolean) throws the exception.appendCodeBlock(String, List, boolean)
public void appendCodeBlock(java.lang.String blockName, java.util.List<java.lang.String> arguments) throws IllegalActionException
blockName
- The name of the code block.arguments
- The user-specified arguments for the code block,
if the code block has parameters.IllegalActionException
- If
appendCodeBlock(String, List, boolean) throws the exception.appendCodeBlock(String, List, boolean)
public void appendCodeBlock(java.lang.String blockName, java.util.List<java.lang.String> arguments, int indentLevel) throws IllegalActionException
blockName
- The name of the code block.arguments
- The user-specified arguments for the code block,
if the code block has parameters.indentLevel
- The level of indention.IllegalActionException
- If
appendCodeBlock(String, List, boolean) throws the exception.appendCodeBlock(String, List, boolean)
public void appendCodeBlock(java.lang.String blockName, java.util.List<java.lang.String> arguments, boolean mayNotExist) throws IllegalActionException
setIndentLevel(int)
.blockName
- The name of the code block.arguments
- The user-specified arguments for the code block,mayNotExist
- Indicate if it is okay not to find the code block.
if the code block has parameters.IllegalActionException
- If _constructCodeTable() throws
the exception, or if the requested code block is required but cannot
be found, or if the numbers of arguments and parameters do not match.public void appendCodeBlock(java.lang.String blockName, java.util.List<java.lang.String> arguments, boolean mayNotExist, int indentLevel) throws IllegalActionException
blockName
- The name of the code block.arguments
- The user-specified arguments for the code block,mayNotExist
- Indicate if it is okay not to find the code block.
if the code block has parameters.indentLevel
- The level of indention.IllegalActionException
- If _constructCodeTable() throws
the exception, or if the requested code block is required but cannot
be found, or if the numbers of arguments and parameters do not match.public java.lang.String getCodeBlock(java.lang.String blockName, java.util.List<java.lang.String> arguments) throws IllegalActionException
blockName
- The given name that identifies the code block.arguments
- The list of arguments to substitute in the code block.IllegalActionException
- Thrown if
getCodeBlock(String, List, boolean) throws it.public java.lang.String getCodeBlock(java.lang.String blockName, java.util.List<java.lang.String> arguments, boolean mayNotExist) throws IllegalActionException
blockName
- The given name that identifies the code block.arguments
- The list of arguments to substitute in the code block.mayNotExist
- False to require the codeblock to exist.IllegalActionException
- Thrown if a problem occurs in
constructing the code block table, or the given code block name
is required to exist but does not.public void appendCodeBlocks(java.lang.String nameExpression) throws IllegalActionException
nameExpression
- The given regular expression for the block names.IllegalActionException
- If _constructCodeTable() throws
the exception, or if the requested code block is required but cannot
be found, or if the numbers of arguments and parameters do not match.public void clear()
public java.lang.String description() throws IllegalActionException
IllegalActionException
- If an error occurs during parsing.public java.util.List<java.lang.String> getAllCodeBlockNames()
public java.util.Set<CodeStream.Signature> getAllCodeBlockSignatures() throws IllegalActionException
IllegalActionException
- If there is a problem when
parsing the code adapter .[target] file.public java.lang.String getCodeBlock(java.lang.String name) throws IllegalActionException
name
- The name of the code block.IllegalActionException
- If a code block by that
name cannot be found.public java.lang.String getCodeBlockTemplate(java.lang.Object signature) throws IllegalActionException
signature
- The signature of the code block.IllegalActionException
- If thrown while getting a code block
template with the name of the signature.public static java.lang.String indent(java.lang.String inputString)
inputString
- The string to be indented.public static java.lang.String indent(int indentLevel, java.lang.String inputString)
indentLevel
- The level of indention.inputString
- The string to be indentedpublic void insert(int offset, java.lang.String code)
offset
- The given position.code
- The given string.public boolean isEmpty()
public static void main(java.lang.String[] args) throws java.io.IOException, IllegalActionException
args
- Command-line arguments, the first of which names a
.[target] file as a URL , for example file:./test/testCodeBlock.c.java.io.IOException
- If an error occurs when reading user inputs.IllegalActionException
- If an error occurs during parsing
the adapter .[target] file.public void parse(java.lang.String filePath) throws IllegalActionException
parse(String, boolean)
.filePath
- The given file path.IllegalActionException
- Thrown if an error occurs when
parsing the code blocks in the file.public void parse(java.lang.String filePath, boolean mayNotExist) throws IllegalActionException
filePath
- The given file path.mayNotExist
- Whether to allow invalid filePath input. True
if the given filePath may not exist (no exception thrown for
invalid file path); false, otherwise.IllegalActionException
- Thrown if an error occurs when
parsing the code blocks in the file.public void reset()
public void setCodeBlocks(java.lang.String codeBlocks)
codeBlocks
- The code blocks to be parsed.public static void selfTest()
public static void setIndentLevel(int indentLevel)
indentLevel
- The indent level, where 0 means no indentation,
1 means indent one level (probably 4 spaces).public java.lang.String toString()
toString
in class java.lang.Object