|
| 1 | +# -*- coding: utf-8 -*- # |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Based on/regexes mostly from Brandon Bennett's pygments-routerlexers: |
| 5 | +# https://github.com/nemith/pygments-routerlexers |
| 6 | + |
| 7 | +module Rouge |
| 8 | + module Lexers |
| 9 | + class CiscoIos < RegexLexer |
| 10 | + title 'Cisco IOS' |
| 11 | + desc 'Cisco IOS configuration lexer' |
| 12 | + tag 'cisco_ios' |
| 13 | + filenames '*.cfg' |
| 14 | + mimetypes 'text/x-cisco-conf' |
| 15 | + |
| 16 | + state :root do |
| 17 | + rule %r/^!.*/, Comment::Single |
| 18 | + |
| 19 | + rule %r/^(version\s+)(.*)$/ do |
| 20 | + groups Keyword, Num::Float |
| 21 | + end |
| 22 | + |
| 23 | + rule %r/(desc*r*i*p*t*i*o*n*)(.*?)$/ do |
| 24 | + groups Keyword, Comment::Single |
| 25 | + end |
| 26 | + |
| 27 | + rule %r/^(inte*r*f*a*c*e*|controller|router \S+|voice translation-\S+|voice-port|line)(.*)$/ do |
| 28 | + groups Keyword::Type, Name::Function |
| 29 | + end |
| 30 | + |
| 31 | + rule %r/(password|secret)(\s+[57]\s+)(\S+)/ do |
| 32 | + groups Keyword, Num, String::Double |
| 33 | + end |
| 34 | + |
| 35 | + rule %r/(permit|deny)/, Operator::Word |
| 36 | + |
| 37 | + rule %r/^(banner\s+)(motd\s+|login\s+)([#$%])/ do |
| 38 | + groups Keyword, Name::Function, Str::Delimiter |
| 39 | + push :cisco_ios_text |
| 40 | + end |
| 41 | + |
| 42 | + rule %r/^(dial-peer\s+\S+\s+)(\S+)(.*?)$/ do |
| 43 | + groups Keyword, Name::Attribute, Keyword |
| 44 | + end |
| 45 | + |
| 46 | + rule %r/^(vlan\s+)(\d+)$/ do |
| 47 | + groups Keyword, Name::Attribute |
| 48 | + end |
| 49 | + |
| 50 | + # IPv4 Address/Prefix |
| 51 | + rule %r/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d{1,2})?/, Num |
| 52 | + |
| 53 | + # NSAP |
| 54 | + rule %r/49\.\d{4}\.\d{4}\.\d{4}\.\d{4}\.\d{2}/, Num |
| 55 | + |
| 56 | + # MAC Address |
| 57 | + rule %r/[a-f0-9]{4}\.[a-f0-9]{4}\.[a-f0-9]{4}/, Num::Hex |
| 58 | + |
| 59 | + rule %r/^(\s*no\s+)(\S+)/ do |
| 60 | + groups Keyword::Constant, Keyword |
| 61 | + end |
| 62 | + |
| 63 | + rule %r/^[^\n\r]\s*\S+/, Keyword |
| 64 | + |
| 65 | + # Obfuscated Passwords |
| 66 | + rule %r/\*+/, Name::Entity |
| 67 | + |
| 68 | + rule %r/(?<= )\d+(?= )/, Num |
| 69 | + |
| 70 | + # Newline catcher, avoid errors on empty lines |
| 71 | + rule %r/\n+/m, Text |
| 72 | + |
| 73 | + # This one goes last, a text catch-all |
| 74 | + rule %r/./, Text |
| 75 | + end |
| 76 | + |
| 77 | + state :cisco_ios_text do |
| 78 | + rule %r/[^#$%]/, Text |
| 79 | + rule %r/[#$%]/, Str::Delimiter, :pop! |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | +end |
0 commit comments