Git Product home page Git Product logo

Comments (5)

engineerjoe440 avatar engineerjoe440 commented on September 28, 2024 1

FWIW, I've been using the method of "constructing" properties in this way, and it seems to be working for the most part.

from blark.

klauer avatar klauer commented on September 28, 2024 1

Glad it's at least mostly worked. I'm honestly not entirely convinced of the method, as I think the above comment touched on.

It's been on the back of my mind to look back into maybe natively supporting .TcPOU (and related) files here without any pytmc translation. I think it'd likely be cleaner in the end, even if it means the grammar has to get restructured to support it. (Don't hold me to this statement, though 😁 )

from blark.

klauer avatar klauer commented on September 28, 2024 1

#64 aims to address this without custom grammar.

Properties from TwinCAT source code files can be parsed piece-by-piece getter/setter declaration/implementation (for a total of 4 different code blocks). Then it's up to you to keep track of which corresponds to the getter and which corresponds to the setter.

Writing a custom blark input handler can be an option here. That PR also tries to make it easier to parse arbitrary individual code sections more easily (even without the input handler) in blark.parse.parse_source_code - starting_source can be specified to a grammar rule such as data_type_declaration or program_declaration (or any other library declarations). new_parser still lets you dig deeper into non-top-level rules.

from blark.

klauer avatar klauer commented on September 28, 2024 1

Properties are reasonably well-supported now (as of v0.7.0) - some tweaks may come down the line. Closing for now

from blark.

klauer avatar klauer commented on September 28, 2024

It's a good question - one I waffled back and forth with for a bit before settling on what's in blark right now. There's a longer discussion - more a monologue perhaps? - here: pcdshub/pytmc#274

The current state is that the grammar allows for this for the getter:

PROPERTY PUBLIC LogToVisualStudio : BOOL
VAR
END_VAR
LogToVisualStudio := bLogToVisualStudio;
END_PROPERTY

And this for the setter:

PROPERTY PUBLIC LogToVisualStudio
VAR
	bValue : BOOL;
END_VAR
THIS^.bLogToVisualStudio := bValue;
END_PROPERTY

Note the return type in the getter and not on the setter.

Now you might then wonder "how do you associate a method or a property or an action with a function block?"
The best I could come up with is that it's parsed-order-dependent. Properties/methods/actions defined as part of of a larger set of source code would implicitly apply to the most-recently-seen function block. That's what this first pass at the summary module does:

blark/blark/summary.py

Lines 789 to 889 in e572219

@staticmethod
def from_source(
code: tf.SourceCode, filename: Optional[pathlib.Path] = None
) -> CodeSummary:
result = CodeSummary()
code_by_lines = [""] + code.raw_source.splitlines()
items = code.items
def get_code_by_meta(meta: Optional[tf.Meta]) -> str:
if not meta:
return ""
return "\n".join(code_by_lines[meta.line:meta.end_line + 1])
last_parent = None
for item in items:
if isinstance(item, tf.FunctionBlock):
summary = FunctionBlockSummary.from_function_block(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
result.function_blocks[item.name] = summary
last_parent = summary
elif isinstance(item, tf.Function):
summary = FunctionSummary.from_function(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
result.functions[item.name] = summary
last_parent = None
elif isinstance(item, tf.DataTypeDeclaration):
if isinstance(item.declaration, tf.StructureTypeDeclaration):
summary = DataTypeSummary.from_data_type(
item.declaration,
source_code=get_code_by_meta(item.declaration.meta),
filename=filename,
)
result.data_types[item.declaration.name] = summary
last_parent = None
elif isinstance(item, tf.Method):
if last_parent is not None:
last_parent.methods.append(
MethodSummary.from_method(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
)
elif isinstance(item, tf.Action):
if last_parent is not None:
last_parent.actions.append(
ActionSummary.from_action(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
)
elif isinstance(item, tf.Property):
if last_parent is not None and isinstance(last_parent, ProgramSummary):
last_parent.properties.append(
PropertySummary.from_property(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
)
elif isinstance(item, tf.GlobalVariableDeclarations):
summary = GlobalVariableSummary.from_globals(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
result.globals[item.name] = summary
# for global_var in summary.declarations.values():
# if not qualified_only:
# result.globals[global_var.name] = summary
# result.globals[global_var.qualified_name] = summary
elif isinstance(item, tf.Program):
summary = ProgramSummary.from_program(
item,
source_code=get_code_by_meta(item.meta),
filename=filename,
)
result.programs[item.name] = summary
last_parent = summary
for name, item in list(result.function_blocks.items()):
if item.extends and not item.squashed:
result.function_blocks[name] = item.squash_base_extends(
result.function_blocks
)
for name, item in list(result.data_types.items()):
if item.extends and not item.squashed:
result.data_types[name] = item.squash_base_extends(
result.data_types
)
return result

None of this is set in stone, though, and I'm definitely open to better suggestions!

from blark.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.