Skip to content
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
5 changes: 3 additions & 2 deletions SUPPORTED_LANGUAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ The table below shows the full list of languages (and corresponding classes/alia
| C3 | c3 | [highlightjs-c3](https://github.com/highlightjs/highlightjs-c3) |
| Cache Object Script | cos, cls | |
| Candid | candid, did | [highlightjs-motoko](https://github.com/rvanasa/highlightjs-motoko) |
| Cap’n Proto | capnproto, capnp | |
| Cap'n Proto | capnproto, capnp | |
| Ceylon | ceylon | |
| Chaos | chaos, kaos | [highlightjs-chaos](https://github.com/chaos-lang/highlightjs-chaos) |
| Chapel | chapel, chpl | [highlightjs-chapel](https://github.com/chapel-lang/highlightjs-chapel) |
| Cisco CLI | cisco | [highlightjs-cisco-cli](https://github.com/BMatheas/highlightjs-cisco-cli) |
| Clojure | clojure, clj | |
| Clojure | clojure, clj, edn | |
| CMake | cmake, cmake.in | |
| COBOL | cobol, standard-cobol | [highlightjs-cobol](https://github.com/otterkit/highlightjs-cobol) |
| CODEOWNERS | codeowners | [highlightjs-codeowners](https://github.com/highlightjs/highlightjs-codeowners) |
Expand Down
140 changes: 140 additions & 0 deletions src/languages/cedar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Language: Cedar
Description: Cedar is a language for writing authorization policies and making authorization decisions based on those policies.
Website: https://www.cedarpolicy.com/
Category: system
*/

/** @type LanguageFn */
export default function(hljs) {
const GLOBALS = {
match: /\b(?:ip|decimal|datetime|duration)(?=\()/,
scope: 'built_in',
};

const VARIABLES = {
match: /\b(?<!\.)(principal|action|resource|context)\b/,
scope: 'variable',
};

const TEMPLATES = {
match: /(?:\?resource|\?principal)\b/,
scope: 'template-variable',
};

const POLICY = {
match: /\b(?<!\.)(permit|forbid|when|unless)\b/,
scope: 'keyword',
};

const KEYWORDS = {
keyword: ['if', 'then', 'else'],
literal: ['true', 'false'],
};

const PUNCTUATION = {
match: /(?:,|;|\.|\[|\]|\(|\)|{|\})/,
scope: 'punctuation',
};

const OPERATORS = {
begin:
/(?<!\w)/.source +
'(' +
[
'&&',
'\\|\\|',
'==',
'!=',
'>=',
'<=',
'>',
'<',
'\\+',
'-',
'\\*',
'in',
'like',
'has',
'is',
].join('|') +
')' +
/(?!\w)/.source,
scope: 'operator',
relevance: 0,
};

const INTEGER = {
scope: 'number',
begin: `0|\\-?[1-9](_?[0-9])*`,
relevance: 0,
};

const ENTITIES = {
match: /(?=\b)(([_a-zA-Z][_a-zA-Z0-9]*::)*[_a-zA-Z][_a-zA-Z0-9]*)(?=::)/,
scope: 'title.class',
};

const ISENTITY = {
match: [/is/, /\s+/, /([_a-zA-Z][_a-zA-Z0-9]*::)*[_a-zA-Z][_a-zA-Z0-9]*/],
scope: {
1: 'operator',
3: 'title.class',
},
};

const METHODS = {
scope: 'title.function.invoke',
begin: `(?=\.)(contains|containsAll|containsAny|isEmpty|getTag|hasTag)(?=\\()`,
relevance: 0,
};

const DECIMAL_METHODS = {
scope: 'title.function.invoke',
begin: `(?=\.)(lessThan|lessThanOrEqual|greaterThan|greaterThanOrEqual)(?=\\()`,
relevance: 0,
};

const IP_METHODS = {
scope: 'title.function.invoke',
begin: `(?=\.)(isIpv4|isIpv6|isLoopback|isMulticast|isInRange)(?=\\()`,
relevance: 0,
};

const DATETIME_METHODS = {
scope: 'title.function.invoke',
begin: `(?=\.)(offset|durationSince|toDate|toTime)(?=\\()`,
relevance: 0,
};

const DURATION_METHODS = {
scope: 'title.function.invoke',
begin: `(?=\.)(toMilliseconds|toSeconds|toMinutes|toHours|toDays)(?=\\()`,
relevance: 0,
};

return {
name: 'Cedar',
aliases: ['cedar'],
case_insensitive: false,
keywords: KEYWORDS,
contains: [
hljs.QUOTE_STRING_MODE,
hljs.C_LINE_COMMENT_MODE,
GLOBALS,
VARIABLES,
POLICY,
INTEGER,
PUNCTUATION,
ENTITIES,
ISENTITY,
OPERATORS,
METHODS,
DECIMAL_METHODS,
IP_METHODS,
DATETIME_METHODS,
DURATION_METHODS,
TEMPLATES,
],
};
}