Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Add Augeas lexer (rouge-ruby#1521)
Browse files Browse the repository at this point in the history
This commit adds a lexer for Augeas.

Co-authored-by: Michael Camilleri <mike@inqk.net>
  • Loading branch information
2 people authored and mattt committed May 19, 2021
1 parent 31dc09c commit 1f8c01e
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/rouge/demos/augeas
@@ -0,0 +1,16 @@
(*
This is a comment
*)
module Foo =
autoload xfm

let a = b | c . d

let lns = a*

let filter = incl "/path/to/file"
. incl "/path/to/other_file"
. Util.stdexcl

(* xmf is the transform *)
let xmf = transform lns filter
93 changes: 93 additions & 0 deletions lib/rouge/lexers/augeas.rb
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class Augeas < RegexLexer
title "Augeas"
desc "The Augeas programming language (augeas.net)"

tag 'augeas'
aliases 'aug'
filenames '*.aug'
mimetypes 'text/x-augeas'

def self.reserved
@reserved ||= Set.new %w(
_ let del store value counter seq key label autoload incl excl
transform test get put in after set clear insa insb print_string
print_regexp print_endline print_tree lens_ctype lens_atype
lens_ktype lens_vtype lens_format_atype regexp_match
)
end

state :basic do
rule %r/\s+/m, Text
rule %r/\(\*/, Comment::Multiline, :comment
end

state :comment do
rule %r/\*\)/, Comment::Multiline, :pop!
rule %r/\(\*/, Comment::Multiline, :comment
rule %r/[^*)]+/, Comment::Multiline
rule %r/[*)]/, Comment::Multiline
end

state :root do
mixin :basic

rule %r/(:)(\w\w*)/ do
groups Punctuation, Keyword::Type
end

rule %r/\w[\w']*/ do |m|
name = m[0]
if name == "module"
token Keyword::Reserved
push :module
elsif self.class.reserved.include? name
token Keyword::Reserved
elsif name =~ /\A[A-Z]/
token Keyword::Namespace
else
token Name
end
end

rule %r/"/, Str, :string
rule %r/\//, Str, :regexp

rule %r([-*+.=?\|]+), Operator
rule %r/[\[\](){}:;]/, Punctuation
end

state :module do
rule %r/\s+/, Text
rule %r/[A-Z][a-zA-Z0-9_.]*/, Name::Namespace, :pop!
end

state :regexp do
rule %r/\//, Str::Regex, :pop!
rule %r/[^\\\/]+/, Str::Regex
rule %r/\\[\\\/]/, Str::Regex
rule %r/\\/, Str::Regex
end

state :string do
rule %r/"/, Str, :pop!
rule %r/\\/, Str::Escape, :escape
rule %r/[^\\"]+/, Str
end

state :escape do
rule %r/[abfnrtv"'&\\]/, Str::Escape, :pop!
rule %r/\^[\]\[A-Z@\^_]/, Str::Escape, :pop!
rule %r/o[0-7]+/i, Str::Escape, :pop!
rule %r/x[\da-f]+/i, Str::Escape, :pop!
rule %r/\d+/, Str::Escape, :pop!
rule %r/\s+/, Str::Escape, :pop!
rule %r/./, Str, :pop!
end
end
end
end
19 changes: 19 additions & 0 deletions spec/lexers/augeas_spec.rb
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Augeas do
let(:subject) { Rouge::Lexers::Augeas.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.aug'
end

it 'guess by mimetype' do
assert_guess :mimetype => 'text/x-augeas'
end
end
end

32 changes: 32 additions & 0 deletions spec/visual/samples/augeas
@@ -0,0 +1,32 @@
(*
Multiline
Comment
*)

module Example =
autoload xfm

let eol = Util.eol
let sep_spc = Sep.space
let sep_cont = Sep.cl_or_space

let sep_cont_opt_build (sep:string) =
del (Rx.cl_or_opt_space . sep . Rx.cl_or_opt_space) (" " . sep . " ")

let negate_or_value (key:lens) (value:lens) =
[ del_negate . (negate_node . key | key . value) ]

let del_negate = del /(!!)*/ ""

let sto_to_com_cmnd = del_negate . negate_node? . (
let alias = Rx.word - /(NO)?(PASSWD|EXEC|SETENV)/
in let non_alias = /[\/a-z]([^,:#()\n\\]|\\\\[=:,\\])*[^,=:#() \t\n\\]|[^,=:#() \t\n\\]/
in store (alias | non_alias))

let filter = (incl "/etc/sudoers")
. (incl "/usr/local/etc/sudoers")
. (incl "/etc/sudoers.d/*")
. (incl "/usr/local/etc/sudoers.d/*")
. (incl "/opt/csw/etc/sudoers")
. (incl "/etc/opt/csw/sudoers")
. Util.stdexcl

0 comments on commit 1f8c01e

Please sign in to comment.