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

Αdding support for Q# language #2804

Merged
merged 21 commits into from
Mar 23, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updates components/prism-qsharp.js with suggested changes
fedonman committed Mar 18, 2021
commit 944ee5b6c696de17e0644e195e42b4661ddfd5bf
51 changes: 15 additions & 36 deletions components/prism-qsharp.js
Original file line number Diff line number Diff line change
@@ -43,62 +43,41 @@
// https://github.com/microsoft/qsharp-language/tree/main/Specifications/Language/5_Grammar
var keywordKinds = {
// keywords which represent a return or variable type
type: 'Unit Int BigInt Double Bool String Qubit Result Range true false Zero One Pauli PauliI PauliX PauliY PauliZ Adj Ctl',
// keywords which are used to declare a type
typeDeclaration: 'newtype',
type: 'Adj BigInt Bool Ctl Double false Int One Pauli PauliI PauliX PauliY PauliZ Qubit Range Result String true Unit Zero',
// all other keywords
other: 'Adjoint adjoint and apply as auto body borrow borrowing Controlled controlled distribute elif else fail fixup for function if in internal intrinsic invert is let mutable namespace new not open operation or repeat return self set until use using while within'
other: 'Adjoint adjoint and apply as auto body borrow borrowing Controlled controlled distribute elif else fail fixup for function if in internal intrinsic invert is let mutable namespace new newtype not open operation or repeat return self set until use using while within'
}
// keywords
function keywordsToPattern(words) {
return '\\b(?:' + words.trim().replace(/ /g, '|') + ')\\b';
}
var typeDeclarationKeywords = keywordsToPattern(keywordKinds.typeDeclaration);
var keywords = RegExp(keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.typeDeclaration + ' ' + keywordKinds.other));
var nonTypeKeywords = keywordsToPattern(keywordKinds.typeDeclaration + ' ' + keywordKinds.other);
var keywords = RegExp(keywordsToPattern(keywordKinds.type + ' ' + keywordKinds.other));
var nonTypeKeywords = keywordsToPattern(keywordKinds.other);

// types
var generic = nested(/<(?:[^<>;=+\-*/%&|^]|<<self>>)*>/.source, 2); // the idea behind the other forbidden characters is to prevent false positives. Same for tupleElement.
var nestedRound = nested(/\((?:[^()]|<<self>>)*\)/.source, 2);
var generic = nested(/<(?:[^<>;=+\-*/%&|^]|<<self>>)*>/.source, 2);
var name = /@?\b[A-Za-z_]\w*\b/.source;
var genericName = replace(/<<0>>(?:\s*<<1>>)?/.source, [name, generic]);
var identifier = replace(/(?!<<0>>)<<1>>(?:\s*\.\s*<<1>>)*/.source, [nonTypeKeywords, genericName]);
var array = /\[\s*(?:,\s*)*\]/.source;
var typeExpressionWithoutTuple = replace(/<<0>>(?:\s*(?:\?\s*)?<<1>>)*(?:\s*\?)?/.source, [identifier, array]);
var tupleElement = replace(/[^,()<>[\];=+\-*/%&|^]|<<0>>|<<1>>|<<2>>/.source, [generic, nestedRound, array])
var tuple = replace(/\(<<0>>+(?:,<<0>>+)+\)/.source, [tupleElement]);
var typeExpression = replace(/(?:<<0>>|<<1>>)(?:\s*(?:\?\s*)?<<2>>)*(?:\s*\?)?/.source, [tuple, identifier, array]);

var typeInside = {
'keyword': keywords,
'punctuation': /[<>()?,.:[\]]/
};

// strings & characters
var character = /'(?:[^\r\n'\\]|\\.|\\[Uux][\da-fA-F]{1,8})'/.source; // simplified pattern
var regularString = /"(?:\\.|[^\\"\r\n])*"/.source;

// attributes
var regularStringOrCharacter = regularString + '|' + character;
var regularStringCharacterOrComment = replace(/\/(?![*/])|\/\/[^\r\n]*[\r\n]|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>/.source, [regularStringOrCharacter]);

Prism.languages.qsharp = Prism.languages.extend('clike', {
'string': [
{
pattern: re(/(^|[^$\\])<<0>>/.source, [regularString]),
lookbehind: true,
greedy: true
},
{
pattern: RegExp(character),
greedy: true,
alias: 'character'
}
],
'string': {
pattern: re(/(^|[^$\\])<<0>>/.source, [regularString]),
lookbehind: true,
greedy: true
},
'class-name': [
{
// open Microsoft.Quantum.Canon;
pattern: re(/(\bopen\s+)<<0>>(?=\s*;)/.source, [identifier]),
pattern: re(/(\b(?:as|open)\s+)<<0>>(?=\s*(?:;|as\b))/.source, [identifier]),
lookbehind: true,
inside: typeInside
},
@@ -110,7 +89,7 @@
},
],
'keyword': keywords,
'number': /(?:0(?:x[\da-f_]*[\da-f]|b[01_]*[01]|o[0-7]*[0-7])|(?:\B\.\d*|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i,
'number': /(?:\b0(?:x[\da-f]+|b[01]+|o[0-7]+)|(?:\B\.\d+|\b\d+(?:\.\d*)?)(?:e[-+]?\d+)?)l?\b/i,
'operator': /and=|or=|<[-=]|[-=]>|[*^=\-!+\/%=]=?|>>>=?|<<<=?|\^\^\^=?|\|\|\|=?|&&&=?|w\/=?|\.\.\.|~~~/,
'punctuation': /::|[{}[\];(),.:]/
});
@@ -126,7 +105,7 @@
var formatString = /:[^}\r\n]+/.source;

// single line
var sInterpolationRound = nested(replace(/[^"'/()]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>|\(<<self>>*\)/.source, [regularStringOrCharacter]), 2)
var sInterpolationRound = nested(replace(/[^"'/()]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|<<0>>|\(<<self>>*\)/.source, [regularString]), 2)
var sInterpolation = replace(/\{(?!\{)(?:(?![}:])<<0>>)*<<1>>?\}/.source, [sInterpolationRound, formatString]);

function createInterpolationInside(interpolation, interpolationRound) {
@@ -145,8 +124,8 @@
'punctuation': /^\{|\}$/,
'expression': {
pattern: /[\s\S]+/,
alias: 'language-csharp',
inside: Prism.languages.csharp
alias: 'language-qsharp',
inside: Prism.languages.qsharp
}
}
},
2 changes: 1 addition & 1 deletion components/prism-qsharp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.