public class TemplateParser
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected ProgramCodeGenerator |
_codeGenerator
The ProgramCodeGenerator, set by calling
init(Object, ProgramCodeGeneratorAdapter) or
setCodeGenerator(ProgramCodeGenerator) . |
protected java.lang.Object |
_component
The component, set by calling
init(Object, ProgramCodeGeneratorAdapter) . |
protected ParseTreeCodeGenerator |
_parseTreeCodeGenerator
The parse tree to use with expressions.
|
Constructor and Description |
---|
TemplateParser()
Construct the TemplateParser associated
with the given component and the given adapter.
|
Modifier and Type | Method and Description |
---|---|
protected static int |
_findClosedParen(java.lang.String string,
int pos)
Find the paired close parenthesis given a string and an index
which is the position of an open parenthesis.
|
(package private) CodeStream |
_getActualCodeStream()
Return the actual CodeStream for this Adapter.
|
(package private) static CodeStream |
_getActualCodeStream(java.lang.Object component,
GenericCodeGenerator codeGenerator)
Return the actual CodeStream associated with the given Actor and
GenericCodeGenerator.
|
protected ProgramCodeGenerator |
_getCodeGenerator()
Get the code generator associated with this adapter class.
|
protected java.lang.String |
_replaceMacro(java.lang.String macro,
java.lang.String parameter)
Return the replacement string of the given macro.
|
void |
addFunctionUsed(java.lang.String functionName)
Add a function to the Set of function used thus far.
|
void |
addNewTypesUsed(java.lang.String typeName)
Add a type to the set of codegen types.
|
static java.lang.String |
escapeName(java.lang.String name)
Escape a port or actor name for use in the macro language.
|
static java.lang.String |
escapePortName(java.lang.String name)
Escape a port name for use in the macro language.
|
java.lang.String |
generateBlockCode(java.lang.String blockName,
java.util.List<java.lang.String> args)
Given a block name, generate code for that block.
|
java.lang.String |
generateTypeConvertMethod(java.lang.String expression,
java.lang.String castType,
java.lang.String refType)
Generate expression that evaluates to a result of equivalent
value with the cast type.
|
java.lang.String |
generateTypeConvertStatement(ProgramCodeGeneratorAdapter.Channel source,
ProgramCodeGeneratorAdapter.Channel sink,
int offset,
java.lang.String alternativeSourceRef)
Generate the type conversion statement for the particular offset of
the two given channels.
|
CodeStream |
getCodeStream()
Return the code stream.
|
java.lang.String |
getFunctionInvocation(java.lang.String functionString,
boolean isStatic)
Return the translated token instance function invocation string.
|
java.util.Set<java.lang.String> |
getHeaderFiles()
Get the files needed by the code generated from this adapter class.
|
java.lang.String |
getNewInvocation(java.lang.String constructorString)
Return the translated new constructor invocation string.
|
java.lang.String |
getParameterValue(java.lang.String name,
NamedObj container)
Return the value or an expression in the target language for
the specified parameter of the associated actor.
|
ParseTreeCodeGenerator |
getParseTreeCodeGenerator()
Return the parse tree to use with expressions.
|
TypedIOPort |
getPort(java.lang.String refName)
Get the port that has the given name.
|
java.lang.String |
getSize(java.lang.String name)
Get the size of a parameter.
|
static int |
indexOf(java.lang.String ch,
java.lang.String string,
int fromIndex)
Return the index of a specific character in the string starting
from the given index.
|
void |
init(java.lang.Object component,
ProgramCodeGeneratorAdapter adapter)
Init the TemplateParser with the associated
given component and the given adapter.
|
static java.util.List<java.lang.String> |
parseList(java.lang.String parameters)
Parse the list of comma separated parameters.
|
java.lang.String |
processCode(java.lang.String code)
Process the specified code, replacing macros with their values.
|
void |
setCodeGenerator(ProgramCodeGenerator codeGenerator)
Set the associated code generator.
|
static java.lang.String |
unescapeName(java.lang.String name)
Unescape a port or actor name so that the return value
may be used to find the port in the model.
|
static java.lang.String |
unescapePortName(java.lang.String name)
Unescape a port name so that the return value
may be used to find the port in the model.
|
protected ProgramCodeGenerator _codeGenerator
init(Object, ProgramCodeGeneratorAdapter)
or
setCodeGenerator(ProgramCodeGenerator)
.protected java.lang.Object _component
init(Object, ProgramCodeGeneratorAdapter)
.protected ParseTreeCodeGenerator _parseTreeCodeGenerator
public TemplateParser()
public void addFunctionUsed(java.lang.String functionName)
functionName
- A string naming the function.public void addNewTypesUsed(java.lang.String typeName)
typeName
- A string naming the type.public static java.lang.String escapeName(java.lang.String name)
unescapeName(String)
.name
- The port or actorname, which may contain "$",
"-" and/or "*".CodeGeneratorAdapter.generateName(NamedObj)
,
unescapePortName(String)
public static java.lang.String escapePortName(java.lang.String name)
unescapePortName(String)
.name
- The port name, which may contain "$",
"-" and/or "*".CodeGeneratorAdapter.generateName(NamedObj)
,
unescapePortName(String)
public java.lang.String generateTypeConvertMethod(java.lang.String expression, java.lang.String castType, java.lang.String refType) throws IllegalActionException
expression
- The given variable expression.castType
- The given cast type.refType
- The given type of the variable.IllegalActionException
- If thrown while processing code.public java.lang.String generateTypeConvertStatement(ProgramCodeGeneratorAdapter.Channel source, ProgramCodeGeneratorAdapter.Channel sink, int offset, java.lang.String alternativeSourceRef) throws IllegalActionException
If alternativeSourceRef is not null, then we use this instead of the source itself. Thus we generate the type conversion statement with alternativeSourceRef on the right side of the equal sign, and a reference to the sink on the left side.
source
- The given source channel.sink
- The given sink channel.offset
- The given offset._generateTypeConvertStatementalternativeSourceRef
- The alternative source reference for
the port. If alternativeSourceRef is null, then the adapter for the port
of the source channel is used.IllegalActionException
- If there is a problem getting the
adapters for the ports or if the conversion cannot be handled.public final CodeStream getCodeStream()
public java.util.Set<java.lang.String> getHeaderFiles() throws IllegalActionException
IllegalActionException
- Not Thrown in this base class.public final java.lang.String getParameterValue(java.lang.String name, NamedObj container) throws IllegalActionException
name
- The name of the parameter.container
- The container to search upwards from.IllegalActionException
- If the parameter does not exist or
does not have a value.public ParseTreeCodeGenerator getParseTreeCodeGenerator()
public static int indexOf(java.lang.String ch, java.lang.String string, int fromIndex)
ch
- The character to search for.string
- The given string to search from.fromIndex
- The index to start the search.public static java.util.List<java.lang.String> parseList(java.lang.String parameters)
parameters
- A comma separate list of parameters.public void init(java.lang.Object component, ProgramCodeGeneratorAdapter adapter)
Calling this method sets the code generator to that of the adapter.
Note that calling setCodeGenerator(ProgramCodeGenerator)
also sets the code generator.
component
- The associated component.adapter
- The associated adapter.public final java.lang.String processCode(java.lang.String code) throws IllegalActionException
$xxx(), where are processed by _replaceMacro(String, String)
${foo}, which means get the value of the parameter "foo"
$nation, which is ignored.
code
- The code to process.IllegalActionException
- If illegal macro names are found.public java.lang.String generateBlockCode(java.lang.String blockName, java.util.List<java.lang.String> args) throws IllegalActionException
blockName
- The name of the block.args
- The arguments for the block.IllegalActionException
- If illegal macro names are
found, or if there is a problem parsing the code block from
the adapter .c file.public java.lang.String getFunctionInvocation(java.lang.String functionString, boolean isStatic) throws IllegalActionException
functionString
- The string within the $tokenFunc() macro.isStatic
- True if the method is static.IllegalActionException
- The given function string is
not well-formed.public java.lang.String getNewInvocation(java.lang.String constructorString) throws IllegalActionException
constructorString
- The string within the $new() macro.IllegalActionException
- The given constructor string is
not well-formed.public final TypedIOPort getPort(java.lang.String refName)
refName
- The given of the port, usually a simple string
like "input". The refName is also processed using
unescapePortName(String)
so that we handle port
names that have "$", "*" and "-".public java.lang.String getSize(java.lang.String name) throws IllegalActionException
name
- The name of the parameter.IllegalActionException
- If no port or parameter of
the given name is found.public final void setCodeGenerator(ProgramCodeGenerator codeGenerator)
init(Object, ProgramCodeGeneratorAdapter)
also sets the code generator.codeGenerator
- The code generator associated with this class._getCodeGenerator()
public static java.lang.String unescapeName(java.lang.String name)
name
- The port or actor name, which may contain "$",
"-" and/or "*".escapePortName(String)
public static java.lang.String unescapePortName(java.lang.String name)
name
- The port name, which may contain "$",
"-" and/or "*".escapePortName(String)
protected ProgramCodeGenerator _getCodeGenerator()
setCodeGenerator(ProgramCodeGenerator)
protected java.lang.String _replaceMacro(java.lang.String macro, java.lang.String parameter) throws IllegalActionException
macro
- The given macro.parameter
- The given parameter to the macro.IllegalActionException
- Thrown if the given macro or
parameter is not valid.protected static int _findClosedParen(java.lang.String string, int pos) throws IllegalActionException
string
- The given string.pos
- The given index.IllegalActionException
- If the character at the
given position of the string is not an open parenthesis or
if the index is less than 0 or past the end of the string.static CodeStream _getActualCodeStream(java.lang.Object component, GenericCodeGenerator codeGenerator) throws IllegalActionException
component
- The actor whose code to return.codeGenerator
- The actor's GenericCodeGenerator.IllegalActionException
- If thrown when getting the actor's adapter.CodeStream _getActualCodeStream() throws IllegalActionException
IllegalActionException
- If thrown by a called method.