Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Other languages are supported via extensions:
[Nemerle](src/lang-n.js);
[Pascal](src/lang-pascal.js);
[Protocol buffers](src/lang-proto.js);
[Puppet](src/lang-puppet.js);
[R, S](src/lang-r.js);
[RD](src/lang-rd.js);
[Rust](src/lang-rust.js);
Expand Down
75 changes: 75 additions & 0 deletions src/lang-puppet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @license
* Copyright (C) 2016 Jorge Morgado
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview
* Registers a language handler for Puppet.
*
* @author jorge at morgado dot ch
*/

// Falls back to 'plain text' for stylesheets that don't style opn and clo.
var PR_OPEN = 'opn pln';
var PR_CLOSE = 'clo pln';

// Falls back to 'attribute name' for stylesheets that don't style fun.
var PR_FUNCTION = 'fun atn';
// Falls back to 'literal' for stylesheets that don't style var.
var PR_VARIABLE = 'var lit';

PR['registerLangHandler'](
PR['createSimpleLexer'](
[
// Open and close
[PR_OPEN, /^[\(\{\[]+/, null, '([{'],
[PR_CLOSE, /^[\)\}\]]+/, null, ')]}'],

Copy link
Copy Markdown

@hlindberg hlindberg May 8, 2016

Choose a reason for hiding this comment

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

There are additional Open/Close pairs:

  • | delimiters around lambda parameters - e.g. $arr.each | $x | { }
  • "spaceship-operators" i.e. <| |>, and <<| |>> for collection operations
  • Heredoc @(TAG) TAG sequence (complex to lex)

// A line comment that starts with #
[PR['PR_COMMENT'], /^#[^\r\n]*/, null, '#'],
// Whitespace
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Puppet also has multi line comments i.e.

/* comment
*/

[PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
// A double or a single quoted string
[PR['PR_STRING'], /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"'],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is not sufficient for interpolation.

[PR['PR_STRING'], /^\'(?:[^\'\\]|\\[\s\S])*(?:\'|$)/, null, "'"]
],
[
// Matching keywords
[PR['PR_KEYWORD'], /^(?:case|class|default|define|else|elsif|false|if|inherits|node|unless|undef|true)\b/, null],
Copy link
Copy Markdown

@hlindberg hlindberg May 8, 2016

Choose a reason for hiding this comment

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

There are additional keywords - here are all the current keywords.

  KEYWORDS = {
    'case'     => [:CASE,     'case',     4],
    'class'    => [:CLASS,    'class',    5],
    'default'  => [:DEFAULT,  'default',  7],
    'define'   => [:DEFINE,   'define',   6],
    'if'       => [:IF,       'if',       2],
    'elsif'    => [:ELSIF,    'elsif',    5],
    'else'     => [:ELSE,     'else',     4],
    'inherits' => [:INHERITS, 'inherits', 8],
    'node'     => [:NODE,     'node',     4],
    'and'      => [:AND,      'and',      3],
    'or'       => [:OR,       'or',       2],
    'undef'    => [:UNDEF,    'undef',    5],
    'false'    => [:BOOLEAN,  false,      5],
    'true'     => [:BOOLEAN,  true,       4],
    'in'       => [:IN,       'in',       2],
    'unless'   => [:UNLESS,   'unless',   6],
    'function' => [:FUNCTION, 'function', 8],
    'type'     => [:TYPE,     'type',     4],
    'attr'     => [:ATTR,     'attr',     4],
    'private'  => [:PRIVATE,  'private',  7],
  }
  APP_MANAGEMENT_KEYWORDS = {
    :with_appm => {
      'application' => [:APPLICATION, 'application',  11],
      'consumes'    => [:CONSUMES,    'consumes',  8],
      'produces'    => [:PRODUCES,    'produces',  8],
      'site'        => [:SITE,        'site',  4]
    },


// Matching types (first letter can be lower or upper case)
// Any of the defined words not followed by ( or =.
[PR['PR_TYPE'], /^(?:[aA]ugeas|[cC](omputer|cron)|[eE]xec|[fF](ile|ilebucket)|[gG]roup|[hH]ost|[iI]nterface|[kK]5login|[mM](acauthorization|ailalias|aillist|cx|ount))\b(?!\s*[\(|=])/, null],
[PR['PR_TYPE'], /^(?:[nN]agios_(command|contact|contactgroup|timeperiod)|[nN]agios_host(|dependency|escalation|textinfo|group)|[nN]agios_service(|dependency|escalation|extinfo|group))\b(?!\s*[\(|=])/, null],
[PR['PR_TYPE'], /^(?:[nN]otify|[pP]ackage|[rR](esources|outer)|[sS]chedule(|_task)|[sS]el(boolean|module)|[sS](ervice|sh_authorized_key|shkey|tage)|[tT]idy|[uU]ser|[vV]lan|[yY]umrepo|[zZ](fs|one|pool))\b(?!\s*[\(|=])/, null],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The above is a hardcoded list of types.
In Puppet a Qualified Reference is a segmented name where every segment starts with UpperCase. e.g. Nagios, File, User, but also types like Integer, Float, String.
A Qualified Name starts with lower case. There is no way to tell a bare word (a string) apart from name of a type - that requires grammar - i.e. when a bare word is followed by a resource instantiation body.

somename { title : <attribute operations> }

IMO - better to leave these plain as it will be wrong a lot of the time. (Unless a lot more work is done to recognize the correct tokens, and also a larger portion of the language's grammar).


// Matching $var, or $::var, or ${::var}, or ${::var1::var2}, etc.
[PR_VARIABLE, /^\$((::)?[a-z]\w*)*((::)?[a-z_]\w*)\b/, null],
[PR_VARIABLE, /^\$\{(?:[a-zA-Zx7f-xff\$]|::)(?:[a-zA-Z0-9_x7f-xff\$]|::)*\}/, null],

// Matching functions
// Any of the defined words not followed by { or =.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This will not work well. This is legal

notice [map, lookup]
$x = search; notice $x

none of the bare words (albeit being names of functions) are actually function references or function calls.

[PR_FUNCTION, /^(?:alert|assert_type|concat|contain|create_resources|crit|debug|defined|digest|each|emerg|epp|err|escape|fail|file|filter|fqdn_rand|generate|gsub|hiera|hiera_array|hiera_hash|hiera_include)\b(?!\s*[{|=])/, null],
[PR_FUNCTION, /^(?:import|include|info|inline_epp|inline_template|lookup|map|match|md5|notice|realize|reduce|regsubst|require|scanf|search|sha1|shellquote|slice|split|sprintf|tag|tagged|template|versioncmp|warning|with)\b(?!\s*[{|=])/, null],

// Matching plain
// A word that ends with =>
[PR['PR_PLAIN'], /^-*(?:[a-z_]|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])\s*(=>)/i],
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Assuming this is for making keywords that appear as attribute names in a resource expression come out as plain. e.g. for something like

someresource { title:
  class => 'some classification'
}

The => is also used in hashes, there keywords are keywords and should not be plain.


// A printable non-space non-special character
[PR['PR_PUNCTUATION'], /^[^\w\t\n\r \xA0()\"\\\';]+/, null]
]),
['puppet']);