Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solidity support revisited #1843

Closed
wants to merge 4 commits into from

Conversation

wmitsuda
Copy link

Fixes #1404 . Revised PR from #1406

Copy link
Member

@RunDevelopment RunDevelopment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @wmitsuda!

I still found a few things which have to be done, so please take a look at my comments.

{
pattern: /\bmapping\s*\(.*\s+=>\s+.*\)(?:\s+(?:private|public|internal|external|inherited))?\s+[A-Za-z_]\w*\b/,
inside: {
keyword: {
Copy link
Member

@RunDevelopment RunDevelopment Mar 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing single quotes.

Suggested change
keyword: {
'keyword': {

}
},
{
pattern: /\bmapping\s*\(.*\s+=>\s+.*\)(?:\s+(?:private|public|internal|external|inherited))?\s+[A-Za-z_]\w*\b/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a problem with .* as the dot also includes most of \s+=>\s+ which invites catastrophic backtracking.
What exactly is .* supposed to match?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first .* is supposed to be a builtin type, the second one may be any type, even another mapping. Don't know if there is a better way to express this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For more information, this is the language specification: https://solidity.readthedocs.io/en/v0.5.7/types.html#mapping-types

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't what the most complex type name could be but wouldn't \w+ instead of .* also work? So \(\s*\w+\s*=>\s*\w+\s*\).

And if we were to allow any content, then \([^()]*\) might be what we want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to have a mapping of mapping, for e.g.: mapping(address => mapping(uint => customStruct[])) someName, and so on... how can I model this recursion on prism? Is there some good example I can follow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching such expression correctly requires a context-free grammar, so we can't match them with regexes. It's fundamentally impossible.
But we can approximate by matching a limited number of recursion levels.

E.g. \((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\) will match recusive bracket expression for up to a maximum depth of 3.

This is how we usually deal with nested expression.

pattern: /\b(?:address|string|bytes\d*|int\d*|uint\d*|bool|u?fixed\d+x\d+)\b(?:\s+(?:indexed\s+)?[A-Za-z_]\w*\s*[,)])?/,
inside: {
'attr-name' : {
pattern: /\b(address|string|bytes\d*|int\d*|uint\d*|bool|u?fixed\d+x\d+)\b(?:\s+(?:indexed\s+)?[A-Za-z_]\w*\s*[,)])?/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure but isn't (?:indexed\s+)? supposed to be part of the lookbehind?

Also, shouldn't \s*[,)] be a lookahead rather than being part of the name?

Why is the whole non-lookbehind expression optional? This might cause empty tokens.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this part is from the original patch from the original author, have to admit I didn't pay much attention to it. I think the original author made a mistake. Not sure exactly what besides a simple attribute declaration he intended to match. I'll remove what I think is unnecessary to make it work and update the patch.

pattern: /\b(?:contract|interface|library|using|struct|function|modifier)\s+[A-Za-z_]\w*(?:\s+is\s+(?:[A-Za-z_][,\s]*)*)?\b/,
inside: {
'variable': {
pattern: /\b(contract|interface|library|using|struct|function|modifier)\s+[A-Za-z_]\w*(?:\s+is\s+(?:[A-Za-z_][,\s]*)*)?\b/,
Copy link
Member

@RunDevelopment RunDevelopment Mar 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the \s+ inside the lookbehind. Tokens should contain leading or trailing spaces.

Suggested change
pattern: /\b(contract|interface|library|using|struct|function|modifier)\s+[A-Za-z_]\w*(?:\s+is\s+(?:[A-Za-z_][,\s]*)*)?\b/,
pattern: /(\b(?:contract|interface|library|using|struct|function|modifier)\s+)[A-Za-z_]\w*(?:\s+is\s+(?:[A-Za-z_][,\s]*)*)?\b/,

@RunDevelopment
Copy link
Member

@mAAdhaTTah

This language would undoubtedly benefit from #1679. Just in case you weren't convinced already 😉

@davide-scalzo
Copy link

Any plan to merge this PR? Is the last blocker to migrate from highlight.js to Prism for us.

@RunDevelopment
Copy link
Member

Since the author never got to make the requested changes, I can make the required changes within the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for Solidity?
4 participants