CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
rust.js327 linesDownload Raw Back to languages
1/*2Language: Rust3Author: Andrey Vlasovskikh <andrey.vlasovskikh@gmail.com>4Contributors: Roman Shmatov <romanshmatov@gmail.com>, Kasper Andersen <kma_untrusted@protonmail.com>5Website: https://www.rust-lang.org6Category: common, system7*/8 9/** @type LanguageFn */10 11function rust(hljs) {12  const regex = hljs.regex;13  // ============================================14  // Added to support the r# keyword, which is a raw identifier in Rust.15  const RAW_IDENTIFIER = /(r#)?/;16  const UNDERSCORE_IDENT_RE = regex.concat(RAW_IDENTIFIER, hljs.UNDERSCORE_IDENT_RE);17  const IDENT_RE = regex.concat(RAW_IDENTIFIER, hljs.IDENT_RE);18  // ============================================19  const FUNCTION_INVOKE = {20    className: "title.function.invoke",21    relevance: 0,22    begin: regex.concat(23      /\b/,24      /(?!let|for|while|if|else|match\b)/,25      IDENT_RE,26      regex.lookahead(/\s*\(/))27  };28  const NUMBER_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';29  const KEYWORDS = [30    "abstract",31    "as",32    "async",33    "await",34    "become",35    "box",36    "break",37    "const",38    "continue",39    "crate",40    "do",41    "dyn",42    "else",43    "enum",44    "extern",45    "false",46    "final",47    "fn",48    "for",49    "if",50    "impl",51    "in",52    "let",53    "loop",54    "macro",55    "match",56    "mod",57    "move",58    "mut",59    "override",60    "priv",61    "pub",62    "ref",63    "return",64    "self",65    "Self",66    "static",67    "struct",68    "super",69    "trait",70    "true",71    "try",72    "type",73    "typeof",74    "union",75    "unsafe",76    "unsized",77    "use",78    "virtual",79    "where",80    "while",81    "yield"82  ];83  const LITERALS = [84    "true",85    "false",86    "Some",87    "None",88    "Ok",89    "Err"90  ];91  const BUILTINS = [92    // functions93    'drop ',94    // traits95    "Copy",96    "Send",97    "Sized",98    "Sync",99    "Drop",100    "Fn",101    "FnMut",102    "FnOnce",103    "ToOwned",104    "Clone",105    "Debug",106    "PartialEq",107    "PartialOrd",108    "Eq",109    "Ord",110    "AsRef",111    "AsMut",112    "Into",113    "From",114    "Default",115    "Iterator",116    "Extend",117    "IntoIterator",118    "DoubleEndedIterator",119    "ExactSizeIterator",120    "SliceConcatExt",121    "ToString",122    // macros123    "assert!",124    "assert_eq!",125    "bitflags!",126    "bytes!",127    "cfg!",128    "col!",129    "concat!",130    "concat_idents!",131    "debug_assert!",132    "debug_assert_eq!",133    "env!",134    "eprintln!",135    "panic!",136    "file!",137    "format!",138    "format_args!",139    "include_bytes!",140    "include_str!",141    "line!",142    "local_data_key!",143    "module_path!",144    "option_env!",145    "print!",146    "println!",147    "select!",148    "stringify!",149    "try!",150    "unimplemented!",151    "unreachable!",152    "vec!",153    "write!",154    "writeln!",155    "macro_rules!",156    "assert_ne!",157    "debug_assert_ne!"158  ];159  const TYPES = [160    "i8",161    "i16",162    "i32",163    "i64",164    "i128",165    "isize",166    "u8",167    "u16",168    "u32",169    "u64",170    "u128",171    "usize",172    "f32",173    "f64",174    "str",175    "char",176    "bool",177    "Box",178    "Option",179    "Result",180    "String",181    "Vec"182  ];183  return {184    name: 'Rust',185    aliases: [ 'rs' ],186    keywords: {187      $pattern: hljs.IDENT_RE + '!?',188      type: TYPES,189      keyword: KEYWORDS,190      literal: LITERALS,191      built_in: BUILTINS192    },193    illegal: '</',194    contains: [195      hljs.C_LINE_COMMENT_MODE,196      hljs.COMMENT('/\\*', '\\*/', { contains: [ 'self' ] }),197      hljs.inherit(hljs.QUOTE_STRING_MODE, {198        begin: /b?"/,199        illegal: null200      }),201      {202        className: 'symbol',203        // negative lookahead to avoid matching `'`204        begin: /'[a-zA-Z_][a-zA-Z0-9_]*(?!')/205      },206      {207        scope: 'string',208        variants: [209          { begin: /b?r(#*)"(.|\n)*?"\1(?!#)/ },210          {211            begin: /b?'/,212            end: /'/,213            contains: [214              {215                scope: "char.escape",216                match: /\\('|\w|x\w{2}|u\w{4}|U\w{8})/217              }218            ]219          }220        ]221      },222      {223        className: 'number',224        variants: [225          { begin: '\\b0b([01_]+)' + NUMBER_SUFFIX },226          { begin: '\\b0o([0-7_]+)' + NUMBER_SUFFIX },227          { begin: '\\b0x([A-Fa-f0-9_]+)' + NUMBER_SUFFIX },228          { begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)'229                   + NUMBER_SUFFIX }230        ],231        relevance: 0232      },233      {234        begin: [235          /fn/,236          /\s+/,237          UNDERSCORE_IDENT_RE238        ],239        className: {240          1: "keyword",241          3: "title.function"242        }243      },244      {245        className: 'meta',246        begin: '#!?\\[',247        end: '\\]',248        contains: [249          {250            className: 'string',251            begin: /"/,252            end: /"/,253            contains: [254              hljs.BACKSLASH_ESCAPE255            ]256          }257        ]258      },259      {260        begin: [261          /let/,262          /\s+/,263          /(?:mut\s+)?/,264          UNDERSCORE_IDENT_RE265        ],266        className: {267          1: "keyword",268          3: "keyword",269          4: "variable"270        }271      },272      // must come before impl/for rule later273      {274        begin: [275          /for/,276          /\s+/,277          UNDERSCORE_IDENT_RE,278          /\s+/,279          /in/280        ],281        className: {282          1: "keyword",283          3: "variable",284          5: "keyword"285        }286      },287      {288        begin: [289          /type/,290          /\s+/,291          UNDERSCORE_IDENT_RE292        ],293        className: {294          1: "keyword",295          3: "title.class"296        }297      },298      {299        begin: [300          /(?:trait|enum|struct|union|impl|for)/,301          /\s+/,302          UNDERSCORE_IDENT_RE303        ],304        className: {305          1: "keyword",306          3: "title.class"307        }308      },309      {310        begin: hljs.IDENT_RE + '::',311        keywords: {312          keyword: "Self",313          built_in: BUILTINS,314          type: TYPES315        }316      },317      {318        className: "punctuation",319        begin: '->'320      },321      FUNCTION_INVOKE322    ]323  };324}325 326export { rust as default };327 
basant307/AI_Governance_Project · CoolFace