|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectptolemy.codegen.kernel.CodeStream
public class CodeStream
Read and process code blocks from the adaptor .[target] file. Adaptor code template files contain c code blocks for the associated java adaptor actor. A proper code block should have the following grammar:
_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:
/*** initBlock ($arg) ***/ if ($ref(input) != $arg) { $ref(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:
if ($ref(input) != 3) { $ref(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.
Yellow (mankit) |
Yellow (mankit) |
Nested Class Summary | |
---|---|
private class |
CodeStream.CodeBlockTable
The code block table class. |
static class |
CodeStream.Signature
Inner class for representing a code block signature. |
Field Summary | |
---|---|
private CodeGeneratorHelper |
_adaptor
The adaptor associated with this code stream. |
private static java.lang.String |
_BLOCKEND
String pattern which represents the end of a code block. |
private static java.lang.String |
_BLOCKSTART
String pattern which represents the start of a code block. |
private java.lang.String |
_codeBlocks
|
protected CodeGenerator |
_codeGenerator
The code generator associated with this code stream. |
private CodeStream.CodeBlockTable |
_declarations
The code block table that stores the code blocks information, like the code body (StringBuffer), signatures, adaptor class associated with the code blocks. |
private boolean |
_doParsing
|
private static java.lang.String |
_eol
End of line character. |
private java.lang.String |
_filePath
The path of the current code template file being parsed. |
private static java.lang.String |
_HEADEREND
String pattern which represents the end of a code block header. |
private static int |
_indentLevel
The indent level. |
private java.lang.String |
_originalFilePath
Original value of _filePath, used for error messages. |
private int |
_parseIndex
Index pointer that indicates the current location within the code template file to be parsed. |
private java.lang.StringBuffer |
_stream
The content of this CodeStream. |
private java.util.List |
_templateArguments
|
private java.util.List |
_templateParameters
|
Constructor Summary | |
---|---|
CodeStream(CodeGeneratorHelper adaptor)
Construct a new code stream associated with the given adaptor. |
|
CodeStream(java.util.List templateArguments,
CodeGeneratorHelper adaptor)
Construct a new code stream associated with the given java actor adaptor. |
|
CodeStream(java.lang.String path,
CodeGenerator generator)
Construct a new code stream, given a specified file path of the adaptor . |
Method Summary | |
---|---|
private static void |
_assert(boolean invariant)
|
private static java.lang.String |
_checkArgumentName(java.lang.String name)
|
private static java.lang.String |
_checkCodeBlockName(java.lang.String name)
Type check the given name and return a well-formed code block name. |
private static java.lang.String |
_checkParameterName(java.lang.String name)
Type check the parameter name. |
private void |
_constructCodeTable(boolean mayNotExist)
Read the code blocks associated with this adaptor and construct the code block and parameter table. |
private void |
_constructCodeTableHelper(boolean mayNotExist)
|
private java.lang.String |
_getPath(java.lang.Class adaptorClass)
Get the file path for the adaptor . |
private boolean |
_needLineInfo()
Return true if the generated source code is bound to the line number and file of the adaptor templates. |
private java.lang.StringBuffer |
_parseBody(java.lang.StringBuffer codeInFile)
Parse from the _parseIndex and return the next code block body from the given StringBuffer. |
private CodeStream.Signature |
_parseCodeBlock(java.lang.StringBuffer codeInFile)
Parse from the _parseIndex for the next single code block and return the code block name. |
private CodeStream.Signature |
_parseHeader(java.lang.StringBuffer codeInFile)
Parse the header of the code block. |
private static java.util.List |
_parseParameterList(java.lang.StringBuffer codeInFile,
int start,
int end)
|
private static java.util.List |
_parseParameterList(java.lang.StringBuffer codeInFile,
int start,
int end,
java.lang.String startSymbol,
java.lang.String endSymbol)
Return a list of parameter expression contains in the given string buffer. |
private static java.lang.StringBuffer |
_substituteParameters(java.lang.StringBuffer codeBlock,
java.util.List parameters,
java.util.List arguments)
Substitute parameters. |
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 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 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 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 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 adaptor . |
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 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 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 code template file. |
static void |
setIndentLevel(int indentLevel)
Set the indent level. |
java.lang.String |
toString()
Return the string representation of the code stream. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private boolean _doParsing
private static final java.lang.String _BLOCKEND
private static final java.lang.String _BLOCKSTART
private static final java.lang.String _HEADEREND
private static int _indentLevel
protected CodeGenerator _codeGenerator
private java.lang.String _codeBlocks
private CodeStream.CodeBlockTable _declarations
private static java.lang.String _eol
private java.lang.String _filePath
private CodeGeneratorHelper _adaptor
private java.lang.String _originalFilePath
private int _parseIndex
private java.lang.StringBuffer _stream
private java.util.List _templateArguments
private java.util.List _templateParameters
Constructor Detail |
---|
public CodeStream(CodeGeneratorHelper adaptor)
adaptor
- The adaptor associated with this code stream,
which is currently ignored.public CodeStream(java.util.List templateArguments, CodeGeneratorHelper adaptor)
templateArguments
- Template arguments to be substituted
in the code. Template arguments begin with "<" and end with ">".adaptor
- The actor adaptor associated with this code stream,
which is currently ignored.public CodeStream(java.lang.String path, CodeGenerator generator)
FileUtilities.openForReading(String, URI, ClassLoader)
,
for example "file:./test/testCodeBlock.c".
path
- The given file path.generator
- The generator associated with this CodeStream.Method Detail |
---|
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 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 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 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 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 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 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() throws IllegalActionException
IllegalActionException
- If there is a problem when
parsing the code adaptor .[target] file.public java.util.Set<CodeStream.Signature> getAllCodeBlockSignatures() throws IllegalActionException
IllegalActionException
- If there is a problem when
parsing the code adaptor .[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 whilegetting 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 indented
public 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 adaptor .[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.private static void _assert(boolean invariant)
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
private static java.lang.String _checkCodeBlockName(java.lang.String name) throws IllegalActionException
name
- The given code block name.
Thrown
- if the given name is not well-formed.
IllegalActionException
private static java.lang.String _checkArgumentName(java.lang.String name) throws IllegalActionException
IllegalActionException
private static java.lang.String _checkParameterName(java.lang.String name) throws IllegalActionException
name
- The given parameter name.
Thrown
- if the given name is not well-formed.
IllegalActionException
private void _constructCodeTable(boolean mayNotExist) throws IllegalActionException
mayNotExist
- Indicate if the file is required to exist.
IllegalActionException
- If an error occurs when parsing the
adaptor .[target] file.private void _constructCodeTableHelper(boolean mayNotExist) throws IllegalActionException
mayNotExist
- Indicate if the file is required to exist.
IllegalActionException
private boolean _needLineInfo() throws IllegalActionException
IllegalActionException
- If there is a problem reading
the CodeGenerator.sourceLineBinding
parameter.private java.lang.String _getPath(java.lang.Class adaptorClass)
adaptorClass
- The given adaptor class
private java.lang.StringBuffer _parseBody(java.lang.StringBuffer codeInFile) throws IllegalActionException
codeInFile
- Code from the adaptor code template file.
IllegalActionException
- If code block's close block
pattern, _BLOCKEND, is missing._parseIndex
,
_BLOCKEND
private CodeStream.Signature _parseCodeBlock(java.lang.StringBuffer codeInFile) throws IllegalActionException
codeInFile
- Code from the adaptor code template file.
IllegalActionException
- If an error occurs during parsing._parseHeader(StringBuffer)
,
_parseBody(StringBuffer)
private CodeStream.Signature _parseHeader(java.lang.StringBuffer codeInFile) throws IllegalActionException
codeInFile
- Code from the adaptor code template file.
IllegalActionException
- If the code block's close header
pattern, _HEADEREND, is missing._HEADEREND
,
_declarations
private static java.util.List _parseParameterList(java.lang.StringBuffer codeInFile, int start, int end)
private static java.util.List _parseParameterList(java.lang.StringBuffer codeInFile, int start, int end, java.lang.String startSymbol, java.lang.String endSymbol)
codeInFile
- The given string buffer.start
- The given start index to start parsing.end
- The given end index to stop parsing.
private static java.lang.StringBuffer _substituteParameters(java.lang.StringBuffer codeBlock, java.util.List parameters, java.util.List arguments) throws IllegalActionException
codeBlock
- The given code block content.parameters
- The given list of parameters.arguments
- The given list of arguments.
IllegalActionException
- Thrown if
_checkParameterName(String) throws it.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |