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

Add jsonnet #505

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"jscodeshift": "^0.7.0",
"json-stringify-safe": "^5.0.1",
"json-to-ast": "^2.1.0",
"jsonnet-astexplorer": "^0.1.6",
"lodash.isequal": "^4.5.0",
"luaparse": "^0.3.0",
"lucene": "^2.1.1",
Expand Down
112 changes: 112 additions & 0 deletions website/src/parsers/jsonnet/codeExample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// C++ style comment
/* C style comment */
# Hash comment

local various_primitives = [
'foo\\bar\"\u00a0', "foo\\bar\"\u00a0",
@'foo\\bar''\"\u00a0', @"foo\\bar""\'\u00a0",
import 'foo\\bar\"\u00a0', import "foo\\bar\"\u00a0",
importstr @'foo\\bar''\"\u00a0', importstr @"foo\\bar""\'\u00a0",
|||
foo\\bar\"\u00a0 bar baz function |
/* still a string */
// still a string
||| didn't do anything
|||,
// empty file
// blank line
// Blank lines at beginning.
|||

content
|||,

|||
foo
|||
surprise!
|||
|||,
/*
|||
ignore in comment
|||
*/
"||| ignore in string",
"x\"\'x\\x\/x\bx\fx\nx\rx\tx",
'x\"\'x\\x\/x\bx\fx\nx\rx\tx',
0, 0e+0, 0e-0, 0e0, 0E+0, 0E-0, 0E0,
0.0, 0.0e+0, 0.0e-0, 0.0e0, 0.0E+0, 0.0E-0, 0.0E0,
10, 10e+0, 10e-0, 10e0, 10E+0, 10E-0, 10E0,
10.0, 10.0e+0, 10.0e-0, 10.0e0, 10.0E+0, 10.0E-0, 10.0E0,
123, 123.456, 789, 1 + 1, 1.05, 1.999e-999,
false, true, null,
] ;

local keywords = [
import "foo",
importstr "foo",
import @"foo",
importstr @'foo',
error "foo",
assert true : "bar"; "baz",
{ f: self, g: super.g, h: $.f },
if true then 0 else 1,
{ [k]: k for k in ["a", "b", "c"] },
function(x=10) x * x,
std.join([], []), // Contains 'in' which should not be highlighted.
];

local array = [0,1,2,3,4,5,6,7,8,9],
arr_copy = array[:],
other_copy = array[::],
from_2 = array[2:],
to_8 = array[:8],
every_second = array[::2],
every_second_from_2 = array[2::2],
every_second_to_8 = array[:8:2],
combi = array[2:8:2];

local myfunc(x=10) = x * x;
local tail(x, c) = if x == 0 then c else tail(x - 1, c + x) tailstrict;

local obj = { g: 100 };
local obj_g = obj.g;

local parens = (100 / (1 + 1));

local operators = [
!true, ~100, 10 - 5, 5 & 3, 4 | 3, 10 ^ 2,
1 == 3, 1 <= 3, 1 >= 3, 1 < 3, 1 > 3,
1 * 10, 1 / 3, 10 % 3,
];


// Compiler template
local CCompiler = {
cFlags: [],
out: "a.out",
local flags_str = std.join(" ", self.cFlags),
local files_str = std.join(" ", self.files),
cmd: "%s %s %s -o %s" % [self.compiler, flags_str, files_str, self.out],
};

// GCC specialization
local Gcc = CCompiler { compiler: "gcc" };

// Another specialization
local Clang = CCompiler { compiler: "clang" };

// Mixins - append flags
local Opt = { cFlags: super.cFlags + ["-O3", "-DNDEBUG"] };
local Dbg = { cFlags: super.cFlags + ["-g"] };

// Output:
{
targets: [
Gcc + { files: ["a.c", "b.c"] },
Clang + { files: ["test.c"], out: "test" },
Clang + Opt + { files: ["test2.c"], out: "test2" },
Gcc + Opt + Dbg + { files: ["foo.c", "bar.c"], out: "baz" },
],
}
208 changes: 208 additions & 0 deletions website/src/parsers/jsonnet/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
//import 'codemirror-mode-jsonnet';

export const id = 'jsonnet';
export const displayName = 'Jsonnet';
export const mimeTypes = ['application/jsonnet'];
export const fileExtension = 'jsonnet';

import CodeMirror from 'codemirror';
CodeMirror.defineMode('jsonnet', () => {
const keywords = {
local: 'keyword',
self: 'keyword',
super: 'keyword',
assert: 'keyword',
function: 'keyword',
if: 'keyword',
then: 'keyword',
else: 'keyword',
for: 'keyword',
in: 'keyword',
tailstrict: 'keyword',
error: 'keyword',
true: 'atom',
false: 'atom',
null: 'atom',
};

return {
token(stream, state) {
// Handle special states:

// In a C-style comment
if (state.cComment) {
if (stream.match(/\*\//)) {
state.cComment = false;
return 'comment';
}
stream.next();
return 'comment';
}

// In a text block (|||)
if (state.textBlock) {
if (stream.match(/$/)) {
stream.next();
return 'string';
}
if (state.textBlockIndent == null) {
if (stream.match(/\s*\|\|\|/)) {
state.textBlock = false;
return 'string';
}
state.textBlockIndent = stream.indentation();
}
if (
state.textBlockIndent != null &&
stream.indentation() >= state.textBlockIndent
) {
stream.skipToEnd();
return 'string';
}
if (stream.match(/\s*\|\|\|/)) {
state.textBlock = false;
return 'string';
}
stream.next();
return 'error';
}

// In a string (all 4 variants)
if (state.string || state.importString) {
let mode = state.string ? 'string' : 'meta';
if (state.stringRaw) {
if (stream.match(state.stringSingle ? /''/ : /""/)) {
return 'string-2';
}
} else {
if (stream.match(/\\[\\"'\/bfnrt0]/)) {
return 'string-2';
}
if (stream.match(/\\u[0-9a-fA-F]{4}/)) {
return 'string-2';
}
if (stream.match(/\\/)) {
return 'error';
}
}
if (stream.match(state.stringSingle ? /'/ : /"/)) {
state.string = false;
state.importString = false;
state.stringRaw = false;
state.stringDouble = false;
return mode;
}
stream.next();
return mode;
}

// Regular (whole token at a time) processing:

// Comments.
if (stream.match(/\/\//) || stream.match(/#/)) {
stream.skipToEnd();
return 'comment';
}
if (stream.match(/\/\*/)) {
state.cComment = true;
return 'comment';
}

// Imports (including the strings after them).
if (stream.match(/import(?:str)?\s*"/)) {
state.importString = true;
state.stringSingle = false;
state.stringRaw = false;
return 'meta';
}

if (stream.match(/import(?:str)?\s*'/)) {
state.importString = true;
state.stringSingle = true;
state.stringRaw = false;
return 'meta';
}

if (stream.match(/import(?:str)?\s*@"/)) {
state.importString = true;
state.stringSingle = false;
state.stringRaw = true;
return 'meta';
}

if (stream.match(/import(?:str)?\s*@'/)) {
state.importString = true;
state.stringSingle = true;
state.stringRaw = true;
return 'meta';
}

// Strings (without imports)
if (stream.match(/"/)) {
state.string = true;
state.stringSingle = false;
state.stringRaw = false;
return 'string';
}

if (stream.match(/'/)) {
state.string = true;
state.stringSingle = true;
state.stringRaw = false;
return 'string';
}

if (stream.match(/@"/)) {
state.string = true;
state.stringSingle = false;
state.stringRaw = true;
return 'string';
}

if (stream.match(/@'/)) {
state.string = true;
state.stringSingle = true;
state.stringRaw = true;
return 'string';
}

// Enter text block.
if (stream.match(/\|\|\|/)) {
state.textBlock = true;
state.textBlockIndent = null;
return 'string';
}

if (stream.match(/\$/)) return 'keyword';
if (stream.match(/(?:\.\d+|\d+\.?\d*)(?:e[-+]?\d+)?/i)) return 'number';
if (stream.match(/[-+\/*=<>!&~^|$%]+/)) return 'operator';

// Identifiers and keywords that look like identifiers.
let identifier = stream.match(/[a-zA-Z_][a-zA-Z0-9_]*/);
if (identifier) {
identifier = identifier[0];
// If it's not in the dict, we return null to indicate no special
// syntax highlighting.
return keywords.hasOwnProperty(identifier)
? keywords[identifier]
: undefined;
}

stream.next();

return null;
},

startState() {
return {
cComment: false,
textBlock: false,
importString: false,
string: false,
stringSingle: false,
stringRaw: false,
};
},
};
});
CodeMirror.defineMIME('text/x-jsonnet', 'jsonnet');
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//import defaultParserInterface from '../utils/defaultParserInterface';
import pkg from 'jsonnet-astexplorer/package.json';

const ID = 'yolodev-jsonnet-core-lang';

export default {
id: ID,
displayName: 'yolodev-core-lang',
version: pkg.version,
defaultParserID: 'yolodev-jsonnet',

loadTransformer(callback) {
require(['jsonnet-astexplorer'], callback);
},

transform(jsonnet, transformCode, code) {
return jsonnet.desugar(code);
},
};
30 changes: 30 additions & 0 deletions website/src/parsers/jsonnet/yolodev-jsonnet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import defaultParserInterface from '../utils/defaultParserInterface';
import pkg from 'jsonnet-astexplorer/package.json';

const ID = 'yolodev-jsonnet';

export default {
...defaultParserInterface,

id: ID,
displayName: ID,
version: pkg.version,
_ignoredProperties: new Set(['span', 'type']),
locationProps: new Set(['span']),

loadParser(callback) {
require(['jsonnet-astexplorer'], callback);
},

parse(parser, code) {
return parser.parse(code);
},

getNodeName(node) {
return node.type;
},

nodeToRange(node) {
return node.span;
},
};
5 changes: 5 additions & 0 deletions website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6552,6 +6552,11 @@ jsonify@~0.0.0:
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=

jsonnet-astexplorer@^0.1.6:
version "0.1.6"
resolved "https://registry.yarnpkg.com/jsonnet-astexplorer/-/jsonnet-astexplorer-0.1.6.tgz#70b98ad46f9be5e4df3f50f13a52ebf03a6e0577"
integrity sha512-49y0hDK0VRZ+ORo2tDdXNmDTyov4Prr4pRgWS2YF389aibMXsUhG0A6im5KMPzIeRCGqW/UEfWkGf5FCtPqMpA==

jsonpointer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.0.1.tgz#4fd92cb34e0e9db3c89c8622ecf51f9b978c6cb9"
Expand Down