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

New language: CIL #1593

Merged
merged 31 commits into from
Nov 28, 2018
Merged

New language: CIL #1593

merged 31 commits into from
Nov 28, 2018

Conversation

sbrl
Copy link
Contributor

@sbrl sbrl commented Oct 23, 2018

I discovered that Prism.js doesn't have CIL (Common Intermediate Language, from .NET) support when I wrote a blog post that contained some - so I decided to create my own grammar, since the process looked fairly easy (it was!).

@mAAdhaTTah
Copy link
Member

Couple of quick notes:

  1. Filename should be prism-cil.js, to match the rest of the languages.
  2. Run gulp to generate the minfied version.
  3. Update the components.js file with the language information.

@sbrl
Copy link
Contributor Author

sbrl commented Oct 23, 2018

Done - thanks for the comment, @mAAdhaTTah! I was wondering how that worked.

Looks like you've got some vulnerabilities in your required npm packages though:

terminal_080

npm audit fix should do the trick to fix that.

@@ -0,0 +1,26 @@
Prism.languages.cil = {
'displayTitle': "CIL",
Copy link
Member

Choose a reason for hiding this comment

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

displayTitle doesn't go here, you already have it in the components.json.

@mAAdhaTTah
Copy link
Member

Thanks for the contribution. One minor comment, but I'll need someone with stronger regex skills to give this a full review. Also, if this is a .NET extension, should this extend from the csharp language?

@RunDevelopment
Copy link
Member

CIL is a language on its own and C# is to CIL basically what C is to Assembly; Namely, C# compiles to CIL.
So, I don't think that extending C# is going to help very much.

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.

First of all, thank you for this amazing PR.

But there are a few things that have to be done before we can merge this:

  1. Please remove the package-lock.json.
  2. Please remove the small changes made to prism.js.

I left you a few comments and questions. Also, it'd be great if you were to add some tests.

Prism.languages.cil = {
'displayTitle': "CIL",

'comment': /\/\/[^\n]*(?=\r?\n)/,
Copy link
Member

Choose a reason for hiding this comment

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

Because [^\n]* is greedy it will also consume the \r at the end of a line.
The pattern /\/\/.*/ will do what you want and is really simple.

Copy link
Member

Choose a reason for hiding this comment

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

Also, does CIL support multi-line comments?

components/prism-cil.js Outdated Show resolved Hide resolved

'class-name': {
lookbehind: true,
pattern: /([^a-zA-Z0-9])\.[a-z]+\b/
Copy link
Member

Choose a reason for hiding this comment

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

This is really nitpicky but we usually have the pattern before everything else (namely: lookbehind).

'number': /\b(0x)?[0-9a-fA-F]+\b/i,
'punctuation': /[{}[\];(),:]|IL_[0-9A-Za-z]+/,

'keyword': /\b(iant|currency|syschar|void|bool|int8|int16|int32|int64|float32|float64|error|unsigned int8|unsigned int16|unsigned int32|unsigned int64|nativeType|decimal|date|bstr|lpstr|lpwstr|lptstr|objectref|iunknown|idispatch|struct|interface|int|unsigned int|nested struct|byvalstr|ansi bstr|tbstr|variant bool|lpstruct|static|public|private|family|final|specialname|virtual|abstract|assembly|default|cil managed|famandassem|famorassem|privatescope|hidebysig|newslot|rtspecialname|unmanagedexp|reqsecobj|pinvokeimpl|value|enum|interface|sealed|abstract|auto|sequential|explicit|ansi|unicode|autochar|import|serializable|nested public|nested private|nested family|nested assembly|nested famandassem|nested famorassem|beforefieldinit|specialname|rtspecialname|class|instance|string|extern|extends)\b/,
Copy link
Member

Choose a reason for hiding this comment

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

A few things about this keyword list:

  1. initonly is missing.
  2. Please split things like unsigned int32 into unsigned and int32. This will reduce the number of keywords.
  3. Please sort the keywords alphabetically. This makes the list easier to maintain and you will notice the keywords which are included twice.


'boolean': /\b(?:true|false)\b/,
'number': /\b(0x)?[0-9a-fA-F]+\b/i,
'punctuation': /[{}[\];(),:]|IL_[0-9A-Za-z]+/,
Copy link
Member

Choose a reason for hiding this comment

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

. and = should also be punctuation.

'variable': /\[\w+\]/,

'boolean': /\b(?:true|false)\b/,
'number': /\b(0x)?[0-9a-fA-F]+\b/i,
Copy link
Member

Choose a reason for hiding this comment

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

What about floating point numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I'll update now.


'keyword': /\b(iant|currency|syschar|void|bool|int8|int16|int32|int64|float32|float64|error|unsigned int8|unsigned int16|unsigned int32|unsigned int64|nativeType|decimal|date|bstr|lpstr|lpwstr|lptstr|objectref|iunknown|idispatch|struct|interface|int|unsigned int|nested struct|byvalstr|ansi bstr|tbstr|variant bool|lpstruct|static|public|private|family|final|specialname|virtual|abstract|assembly|default|cil managed|famandassem|famorassem|privatescope|hidebysig|newslot|rtspecialname|unmanagedexp|reqsecobj|pinvokeimpl|value|enum|interface|sealed|abstract|auto|sequential|explicit|ansi|unicode|autochar|import|serializable|nested public|nested private|nested family|nested assembly|nested famandassem|nested famorassem|beforefieldinit|specialname|rtspecialname|class|instance|string|extern|extends)\b/,

'function': /\b(add|add\.ovf|add\.ovf\.un|and|arglist|beq|beq\.s|bge|bge\.s|bge\.un|bge\.un\.s|bgt|bgt\.s|bgt\.un|bgt\.un\.s|ble|ble\.s|ble\.un|ble\.un\.s|blt|blt\.s|blt\.un|blt\.un\.s|bne\.un|bne\.un\.s|box|br|br\.s|break|brfalse|brfalse\.s|brinst|brinst\.s|brnull|brnull\.s|brtrue|brtrue\.s|brzero|brzero\.s|call|calli|callvirt|castclass|ceq|cgt|cgt\.un|ckfinite|clt|clt\.un|constrained\.|conv\.i|conv\.i1|conv\.i2|conv\.i4|conv\.i8|conv\.ovf\.i|conv\.ovf\.i\.un|conv\.ovf\.i1|conv\.ovf\.i1\.un|conv\.ovf\.i2|conv\.ovf\.i2\.un|conv\.ovf\.i4|conv\.ovf\.i4\.un|conv\.ovf\.i8|conv\.ovf\.i8\.un|conv\.ovf\.u|conv\.ovf\.u\.un|conv\.ovf\.u1|conv\.ovf\.u1\.un|conv\.ovf\.u2|conv\.ovf\.u2\.un|conv\.ovf\.u4|conv\.ovf\.u4\.un|conv\.ovf\.u8|conv\.ovf\.u8\.un|conv\.r\.un|conv\.r4|conv\.r8|conv\.u|conv\.u1|conv\.u2|conv\.u4|conv\.u8|cpblk|cpobj|div|div\.un|dup|endfault|endfilter|endfinally|initblk|initobj|isinst|jmp|ldarg|ldarg\.0|ldarg\.1|ldarg\.2|ldarg\.3|ldarg\.s|ldarga|ldarga\.s|ldc\.i4|ldc\.i4\.[0-9]+|ldc\.i4\.m1|ldc\.i4\.M1|ldc\.i4\.s|ldc\.i8|ldc\.r4|ldc\.r8|ldelem|ldelem\.i|ldelem\.i1|ldelem\.i2|ldelem\.i4|ldelem\.i8|ldelem\.r4|ldelem\.r8|ldelem\.ref|ldelem\.u1|ldelem\.u2|ldelem\.u4|ldelem\.u8|ldelema|ldfld|ldflda|ldftn|ldind\.i|ldind\.i1|ldind\.i2|ldind\.i4|ldind\.i8|ldind\.r4|ldind\.r8|ldind\.ref|ldind\.u1|ldind\.u2|ldind\.u4|ldind\.u8|ldlen|ldloc|ldloc\.[0-9]+|ldloc\.s|ldloca|ldloca\.s|ldnull|ldobj|ldsfld|ldsflda|ldstr|ldtoken|ldvirtftn|leave|leave\.s|localloc|mkrefany|mul|mul\.ovf|mul\.ovf\.un|neg|newarr|newobj|nop|not|or|pop|readonly\.|refanytype|refanyval|rem|rem\.un|ret|rethrow|shl|shr|shr\.un|sizeof|starg|starg\.s|stelem|stelem\.i|stelem\.i1|stelem\.i2|stelem\.i4|stelem\.i8|stelem\.r4|stelem\.r8|stelem\.ref|stfld|stind\.i|stind\.i1|stind\.i2|stind\.i4|stind\.i8|stind\.r4|stind\.r8|stind\.ref|stloc|stloc\.0|stloc\.1|stloc\.2|stloc\.3|stloc\.s|stobj|stsfld|sub|sub\.ovf|sub\.ovf\.un|switch|tail\.|throw|unaligned\.|alignment|unbox|unbox\.any|volatile\.|xor)\b/
Copy link
Member

Choose a reason for hiding this comment

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

  1. Why do some end with .?
  2. Please sort this list by descending length.
    Because of the way alternations work and the fact that . is not included in \w (which is why \b won't help), some function names are not going to be matched correctly.
    E.g.: add.ovf will be matched as [add].ovf.
    This is because alternations will use the first one that matches (in this case add).
  3. Please compress the list a little.
    There is a lot of redundancy which can be used to make the list smaller.
    E.g.: All functions which start with conv can be matched with the following pattern: conv\.(?:[iu][1248]?|ovf\.[iu][1248]?(?:\.un)?|r\.un|r4|r8).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. I originally pulled out of the Wikipedia article with a bit of JS and a regex or two to process it.

components.json Outdated
@@ -57,6 +57,10 @@
},
"option": "default"
},
"cil": {
Copy link
Member

Choose a reason for hiding this comment

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

You have to place this further down below (under C++, I think).
These here are special languages for Prism.

@sbrl
Copy link
Contributor Author

sbrl commented Oct 24, 2018

All done! Is that better?

@RunDevelopment
Copy link
Member

Looks good!

There are still a few things I noticed:

  1. Please use non-capturing groups, if the group is not used as a lookbehind.
  2. Again, please remove the package-lock.json and revert the changes made to prism.js. They simply do not belong to this PR.
  3. Could you please consider adding some tests? It'd be great to have a test to cover all functions and keywords to verify that everything is matched correctly.

@sbrl
Copy link
Contributor Author

sbrl commented Oct 24, 2018

I've made the changes requested (even though it's recommended that you commit package-lock.json).

How do I add tests? Is there any documentation on this?

@RunDevelopment
Copy link
Member

Here's the documentation on how to add tests.

@sbrl
Copy link
Contributor Author

sbrl commented Oct 26, 2018

Ah, thanks!

Looking at it, I think test cases are going to be challenging as I don't know the language well enough to create a valid cut-down program that would do the job as a test case. In addition, there's a lot of boilerplate code that's appears to be common across multiple IL files, which further complicates the process of writing tests.

Thoughts?

@RunDevelopment
Copy link
Member

RunDevelopment commented Oct 26, 2018

Prism test cases usually test specific features (e.g. numbers, comments, small aspects of a language in general). So you don't have to come up with a minimal program which includes all features. Just write small tests which test one feature each.

For examples on how to do tests right, I suggest looking at the JavaScript and C-like tests.

@RunDevelopment
Copy link
Member

While playing around I also found a few minor things:

  1. object, method (for overwrites), native (IntPtr), literal (consts and enums), valuetype (enums), runtime (delegates), and implements (interfaces) should be keywords.
  2. try-catch/try-finally blocks don't have catch/finally highlighted.

@sbrl
Copy link
Contributor Author

sbrl commented Nov 9, 2018

Thanks for the comments! PR updated. This is a much longer process than I thought it would be :P

@RunDevelopment
Copy link
Member

@sbrl Looks good!

I found another missing keyword :) char. Also, all of the unsigned integers are missing, so uint and all uint<N>.
(And please update the keyword test to include the newly added ones.)

Don't worry, this is going to be the last nit.

@sbrl
Copy link
Contributor Author

sbrl commented Nov 13, 2018

Done, @RunDevelopment

@RunDevelopment
Copy link
Member

Looks good! Tests pass. @mAAdhaTTah I think we are ready to merge this.

@sbrl
Copy link
Contributor Author

sbrl commented Nov 26, 2018

What's the status on this please, @RunDevelopment? I thought I'd done everything required, but it's been a while and this hasn't been merged yet? Did I miss something?

@RunDevelopment
Copy link
Member

RunDevelopment commented Nov 27, 2018

@sbrl You've done everything we asked of and you definitely have my OK.
It's just that @mAAdhaTTah seems to be quite busy at the moment and hasn't had the time to look at changes himself.

So don't worry! This has not been forgotten.

@mAAdhaTTah
Copy link
Member

@RunDevelopment @sbrl I haven't forgotten! Holidays are busy :). I want to take a pass through Prism's PRs sometime this week(end) so I'll be taking a closer look at this soon.

@mAAdhaTTah
Copy link
Member

@sbrl If you can resolve the conflict (just merge up from master & rerun gulp), I can merge this.

@sbrl
Copy link
Contributor Author

sbrl commented Nov 28, 2018

Done, @mAAdhaTTah :D

@mAAdhaTTah mAAdhaTTah merged commit 38def33 into PrismJS:master Nov 28, 2018
@mAAdhaTTah
Copy link
Member

@sbrl Thank you for the contribution!

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

Successfully merging this pull request may close these issues.

None yet

3 participants