Simplified language definitions.

This commit is contained in:
Aaronepower 2016-10-04 00:01:43 +01:00
parent f0287837b3
commit 5fab02a76d
11 changed files with 1580 additions and 764 deletions

165
Cargo.lock generated
View file

@ -6,15 +6,16 @@ dependencies = [
"encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"handlebars 0.21.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"maplit 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"maplit 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_cbor 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen 0.8.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
@ -33,19 +34,6 @@ name = "ansi_term"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "aster"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_syntax 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "0.7.0"
@ -63,13 +51,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"term_size 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -148,7 +136,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.77 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.79 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -156,6 +144,19 @@ name = "glob"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "handlebars"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"pest 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"quick-error 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.16 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "itoa"
version = "0.1.1"
@ -172,7 +173,7 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.16"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -182,7 +183,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "maplit"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -190,12 +191,12 @@ name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.1.35"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -203,35 +204,39 @@ name = "num_cpus"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quasi"
version = "0.19.0"
name = "pest"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "post-expansion"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_errors 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quasi_codegen"
version = "0.19.0"
name = "quick-error"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "quote"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aster 0.26.1 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_errors 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -246,19 +251,19 @@ dependencies = [
[[package]]
name = "regex"
version = "0.1.77"
version = "0.1.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.3.5"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -273,7 +278,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
version = "0.8.8"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -287,44 +292,43 @@ dependencies = [
[[package]]
name = "serde_codegen"
version = "0.8.8"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aster 0.26.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quasi_codegen 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen_internals 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"post-expansion 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_codegen_internals 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_codegen_internals"
version = "0.8.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_errors 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_json"
version = "0.8.2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_yaml"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.16 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -333,30 +337,39 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syntex"
version = "0.43.0"
name = "syn"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_errors 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syntex"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"syntex_errors 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_syntax 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syntex_errors"
version = "0.43.0"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_pos 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_pos 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syntex_pos"
version = "0.43.0"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
@ -364,15 +377,15 @@ dependencies = [
[[package]]
name = "syntex_syntax"
version = "0.43.0"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_errors 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_pos 0.43.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_errors 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex_pos 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -399,7 +412,7 @@ name = "term_size"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -408,7 +421,7 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -424,7 +437,7 @@ name = "toml"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.8.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -473,6 +486,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "yaml-rust"
version = "0.3.3"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"

View file

@ -16,17 +16,23 @@ name = "tokei"
path = "src/main.rs"
[build-dependencies]
serde_json = "~0.8.0"
serde = "~0.8.0"
[build-dependencies.serde_codegen]
optional = true
version = "0.8.0"
[build-dependencies.handlebars]
version = "0.21.1"
features = ["serde_type"]
[dependencies]
encoding = "0.2.33"
glob = "~0.2.11"
log = "0.3.6"
maplit = "~0.1.3"
rayon = "0.4.2"
rayon = "=0.4.2"
walkdir = "~0.1.5"
[dependencies.clap]

View file

@ -1,5 +1,12 @@
#[cfg(feature = "io")]
extern crate serde_codegen;
extern crate serde;
extern crate serde_json;
extern crate handlebars;
use serde_json::Value;
use handlebars::{Context, Handlebars};
use std::fs::File;
fn main() {
expand();
@ -10,6 +17,7 @@ fn expand() {
use std::env;
use std::path::Path;
use std::thread;
render_handlebars();
let builder = thread::Builder::new()
.name(String::from("Build Thread"))
@ -28,4 +36,18 @@ fn expand() {
}
#[cfg(not(feature = "io"))]
fn expand() {}
fn expand() {
render_handlebars();
}
fn render_handlebars() {
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
let raw_data: Value = serde_json::from_reader(File::open(&"src/lib/languages.json").unwrap()).unwrap();
let data = Context::wraps(&raw_data);
let mut source_template = File::open(&"src/lib/language/language_type.rs.hbs").expect("Can't find Template");
let mut output_file = File::create("src/lib/language/language_type.rs").expect("Can't create!");
if let Err(err) = handlebars.template_renderw2(&mut source_template, &data, &mut output_file) {
panic!("Failed to generate languages! ERROR: {:?}", err);
}
}

1
src/lib/language/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
language_type.rs

View file

@ -166,6 +166,21 @@ impl Language {
Self::new_single(vec!["#"])
}
/// Convience constructor for creating a language that has the same commenting syntax as
/// Haskell.
///
/// ```
/// # use tokei::*;
/// let haskell = Language::new(vec!["--"], vec![("{-", "-}")]).nested();
/// let idris = Language::new_haskell();
///
/// assert_eq!(haskell.line_comment, haskell.line_comment);
/// assert_eq!(haskell.multi_line, haskell.multi_line);
/// ```
pub fn new_haskell() -> Self {
Self::new(vec!["--"], vec![("{-", "-}")]).nested()
}
/// Convience constructor for creating a language that only has multi line comments.
///
/// ```

View file

@ -1,520 +0,0 @@
// Copyright (c) 2015 Aaron Power
// Use of this source code is governed by the APACHE2.0/MIT licence that can be
// found in the LICENCE-{APACHE/MIT} file.
use std::borrow::Cow;
use std::fmt;
use std::path::Path;
use utils::fs;
use self::LanguageType::*;
#[cfg_attr(feature = "io", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum LanguageType {
/// ActionScript
ActionScript,
/// Ada
Ada,
/// Assembly
Assembly,
/// ASP
Asp,
/// ASP.Net
AspNet,
/// Autoconf
Autoconf,
/// Bash
Bash,
/// Batch
Batch,
/// C
C,
/// CHeader
CHeader,
/// Clojure
Clojure,
/// CoffeeScript
CoffeeScript,
/// ColdFusion
ColdFusion,
/// ColdFusionScript
ColdFusionScript,
/// Coq
Coq,
/// Cpp
Cpp,
/// CppHeader
CppHeader,
/// CSharp
CSharp,
/// CShell
CShell,
/// Css
Css,
/// D
D,
/// Dart
Dart,
/// DeviceTree
DeviceTree,
/// Elm
Elm,
/// Erlang
Erlang,
/// Forth
Forth,
/// FortranLegacy
FortranLegacy,
/// FortranModern
FortranModern,
/// GLSL
Glsl,
/// Go
Go,
/// Handlebars
Handlebars,
/// Haskell
Haskell,
/// Html
Html,
/// HEX
Hex,
/// Idris
Idris,
/// Intel HEX
IntelHex,
/// Isabelle
Isabelle,
/// Jai
Jai,
/// Java
Java,
/// JavaScript
JavaScript,
/// Julia
Julia,
/// Json
Json,
/// Jsx
Jsx,
/// Kotlin
Kotlin,
/// Lean
Lean,
/// Less
Less,
/// LinkerScript
LinkerScript,
/// Lisp
Lisp,
/// Lua
Lua,
/// Makefile
Makefile,
/// Markdown
Markdown,
/// Mustache
Mustache,
/// Nim
Nim,
/// ObjectiveC
ObjectiveC,
/// ObjectiveCpp
ObjectiveCpp,
/// OCaml
OCaml,
/// Oz
Oz,
/// Pascal
Pascal,
/// Perl
Perl,
/// Polly
Polly,
/// Php
Php,
/// Protobuf
Protobuf,
/// Prolog
Prolog,
/// Python
Python,
/// Qcl
Qcl,
/// R
R,
/// Razor
Razor,
/// Ruby
Ruby,
/// RubyHtml
RubyHtml,
/// Rust
Rust,
/// ReStructuredText
ReStructuredText,
/// Sass
Sass,
/// Scala
Scala,
/// Sml
Sml,
/// Sql
Sql,
/// Swift
Swift,
/// Tcl
Tcl,
/// Tex
Tex,
/// Text
Text,
/// Toml
Toml,
/// TypeScript
TypeScript,
/// VimScript
VimScript,
/// UnrealScript
UnrealScript,
/// Wolfram
Wolfram,
/// Xml
Xml,
/// Yaml
Yaml,
/// Zsh
Zsh,
}
impl LanguageType {
/// Returns the display name of a language.
///
/// ```
/// # use tokei::*;
/// let bash = LanguageType::Bash;
///
/// assert_eq!(bash.name(), "BASH");
/// ```
pub fn name(&self) -> &'static str {
match *self {
ActionScript => "ActionScript",
Ada => "Ada",
Assembly => "Assembly",
Asp => "ASP",
AspNet => "ASP.Net",
Autoconf => "Autoconf",
Bash => "BASH",
Batch => "Batch",
C => "C",
CHeader => "C Header",
Clojure => "Clojure",
CoffeeScript => "CoffeeScript",
ColdFusion => "ColdFusion",
ColdFusionScript => "ColdFusion CFScript",
Coq => "Coq",
Cpp => "C++",
CppHeader => "C++ Header",
CSharp => "C#",
CShell => "C Shell",
Css => "CSS",
D => "D",
Dart => "Dart",
DeviceTree => "Device Tree",
Elm => "Elm",
Erlang => "Erlang",
Forth => "Forth",
FortranLegacy => "FORTRAN Legacy",
FortranModern => "FORTRAN Modern",
Glsl => "GLSL",
Go => "Go",
Handlebars => "Handlebars",
Haskell => "Haskell",
Html => "HTML",
Hex => "HEX",
Idris => "Idris",
IntelHex => "Intel HEX",
Isabelle => "Isabelle",
Jai => "JAI",
Java => "Java",
JavaScript => "JavaScript",
Json => "JSON",
Jsx => "JSX",
Julia => "Julia",
Kotlin => "Kotlin",
Lean => "Lean",
Less => "LESS",
LinkerScript => "LD Script",
Lisp => "LISP",
Lua => "Lua",
Makefile => "Makefile",
Markdown => "Markdown",
Mustache => "Mustache",
Nim => "Nim",
ObjectiveC => "Objective C",
ObjectiveCpp => "Objective C++",
OCaml => "OCaml",
Oz => "Oz",
Pascal => "Pascal",
Perl => "Perl",
Polly => "Polly",
Php => "PHP",
Protobuf => "Protocol Buffers",
Prolog => "Prolog",
Python => "Python",
Qcl => "QCL",
R => "R",
Razor => "Razor",
Ruby => "Ruby",
RubyHtml => "Ruby HTML",
Rust => "Rust",
ReStructuredText => "ReStructuredText",
Sass => "Sass",
Scala => "Scala",
Sml => "Standard ML",
Sql => "SQL",
Swift => "Swift",
Tcl => "TCL",
Tex => "TeX",
Text => "Plain Text",
Toml => "TOML",
TypeScript => "TypeScript",
UnrealScript => "Unreal Script",
VimScript => "Vim Script",
Wolfram => "Wolfram",
Xml => "XML",
Yaml => "YAML",
Zsh => "Zsh",
}
}
/// Get language from it's file extension.
///
/// ```no_run
/// # use tokei::*;
/// let rust = LanguageType::from_extension("./main.rs");
///
/// assert_eq!(rust, Some(LanguageType::Rust));
/// ```
pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> {
if let Some(extension) = fs::get_extension(entry) {
match &*extension {
"as" => Some(ActionScript),
"ada" | "adb" | "ads" | "pad" => Some(Ada),
"asa" | "asp" => Some(Asp),
"asax" | "ascx"| "asmx"| "aspx"| "master"| "sitemap"| "webinfo" => Some(AspNet),
"bash" | "sh" => Some(Bash),
"bat" | "btm" | "cmd" => Some(Batch),
"c" | "ec" | "pgc" => Some(C),
"cc" | "cpp" | "cxx" | "c++" | "pcc" => Some(Cpp),
"cfc" => Some(ColdFusionScript),
"cfm" => Some(ColdFusion),
"clj" => Some(Clojure),
"coffee" => Some(CoffeeScript),
"cs" => Some(CSharp),
"cshtml" => Some(Razor),
"csh" => Some(CShell),
"css" => Some(Css),
"d" => Some(D),
"dart" => Some(Dart),
"dts" | "dtsi" => Some(DeviceTree),
"el" | "lisp" | "lsp" => Some(Lisp),
"elm" => Some(Elm),
"erl" | "hrl" => Some(Erlang),
"4th" | "forth" | "fr" | "frt" | "fth" | "f83" | "fb" | "fpm" | "e4" | "rx" |
"ft" => Some(Forth),
"f" | "for" | "ftn" | "f77" | "pfo" => Some(FortranLegacy),
"f03" | "f08" | "f90" | "f95" => Some(FortranModern),
"vert" | "tesc" | "tese" | "geom" | "frag" | "comp" => Some(Glsl),
"go" => Some(Go),
"h" => Some(CHeader),
"hbs" | "handlebars" => Some(Handlebars),
"hh" | "hpp" | "hxx" => Some(CppHeader),
"hs" => Some(Haskell),
"html" => Some(Html),
"hex" => Some(Hex),
"idr" | "lidr" => Some(Idris),
"ihex" => Some(IntelHex),
"in" => Some(Autoconf),
"jai" => Some(Jai),
"java" => Some(Java),
"jl" => Some(Julia),
"js" => Some(JavaScript),
"json" => Some(Json),
"jsx" => Some(Jsx),
"kt" | "kts" => Some(Kotlin),
"lds" => Some(LinkerScript),
"lean" | "hlean" => Some(Lean),
"less" => Some(Less),
"lua" => Some(Lua),
"m" => Some(ObjectiveC),
"markdown" | "md" => Some(Markdown),
"ml" | "mli" => Some(OCaml),
"mm" => Some(ObjectiveCpp),
"makefile" => Some(Makefile),
"mustache" => Some(Mustache),
"nim" => Some(Nim),
"nb" | "wl" => Some(Wolfram),
"oz" => Some(Oz),
"p" | "pro" => Some(Prolog),
"pas" => Some(Pascal),
"php" => Some(Php),
"pl" => Some(Perl),
"pm" => Some(Perl),
"qcl" => Some(Qcl),
"text" | "txt" => Some(Text),
"polly" => Some(Polly),
"proto" => Some(Protobuf),
"py" => Some(Python),
"r" => Some(R),
"rake" | "rb" => Some(Ruby),
"rhtml" => Some(RubyHtml),
"rs" => Some(Rust),
"rst" => Some(ReStructuredText),
"s" | "asm" => Some(Assembly),
"sass" | "scss" => Some(Sass),
"sc" | "scala" => Some(Scala),
"sml" => Some(Sml),
"sql" => Some(Sql),
"swift" => Some(Swift),
"tcl" => Some(Tcl),
"tex" | "sty" => Some(Tex),
"toml" => Some(Toml),
"ts" => Some(TypeScript),
"thy" => Some(Isabelle),
"uc" | "uci" | "upkg" => Some(UnrealScript),
"v" => Some(Coq),
"vim" => Some(VimScript),
"xml" => Some(Xml),
"yaml" | "yml" => Some(Yaml),
"zsh" => Some(Zsh),
extension => {
warn!("Unknown extension: {}", extension);
None
},
}
} else {
None
}
}
}
impl From<String> for LanguageType {
fn from(from: String) -> Self {
LanguageType::from(&*from)
}
}
impl<'a> From<&'a str> for LanguageType {
fn from(from: &str) -> Self {
match &*from {
"ActionScript" => ActionScript,
"Ada" => Ada,
"Assembly" => Assembly,
"ASP" => Asp,
"ASP.Net" => AspNet,
"Autoconf" => Autoconf,
"Bash" => Bash,
"Batch" => Batch,
"C" => C,
"CHeader" => CHeader,
"Clojure" => Clojure,
"CoffeeScript" => CoffeeScript,
"ColdFusion" => ColdFusion,
"ColdFusionScript" => ColdFusionScript,
"Coq" => Coq,
"Cpp" => Cpp,
"CppHeader" => CppHeader,
"CSharp" => CSharp,
"CShell" => CShell,
"Css" => Css,
"D" => D,
"Dart" => Dart,
"DeviceTree" => DeviceTree,
"Elm" => Elm,
"Erlang" => Erlang,
"Forth" => Forth,
"FortranLegacy" => FortranLegacy,
"FortranModern" => FortranModern,
"GLSL" => Glsl,
"Go" => Go,
"Handlebars" => Handlebars,
"Haskell" => Haskell,
"Html" => Html,
"HEX" => Hex,
"Intel HEX" => IntelHex,
"Idris" => Idris,
"Jai" => Jai,
"Java" => Java,
"JavaScript" => JavaScript,
"Julia" => Julia,
"Json" => Json,
"Jsx" => Jsx,
"Kotlin" => Kotlin,
"Lean" => Lean,
"Less" => Less,
"LinkerScript" => LinkerScript,
"Lisp" => Lisp,
"Lua" => Lua,
"Makefile" => Makefile,
"Markdown" => Markdown,
"Mustache" => Mustache,
"Nim" => Nim,
"ObjectiveC" => ObjectiveC,
"ObjectiveCpp" => ObjectiveCpp,
"OCaml" => OCaml,
"Oz" => Oz,
"Pascal" => Pascal,
"Perl" => Perl,
"Polly" => Polly,
"Php" => Php,
"Protobuf" => Protobuf,
"Prolog" => Prolog,
"Python" => Python,
"Qcl" => Qcl,
"R" => R,
"Razor" => Razor,
"Ruby" => Ruby,
"RubyHtml" => RubyHtml,
"Rust" => Rust,
"ReStructuredText" => ReStructuredText,
"Sass" => Sass,
"Scala" => Scala,
"Sml" => Sml,
"Sql" => Sql,
"Swift" => Swift,
"Tex" => Tex,
"Text" => Text,
"Toml" => Toml,
"TypeScript" => TypeScript,
"VimScript" => VimScript,
"UnrealScript" => UnrealScript,
"Wolfram" => Wolfram,
"Xml" => Xml,
"Yaml" => Yaml,
"Zsh" => Zsh,
_ => unreachable!(),
}
}
}
impl fmt::Display for LanguageType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl<'a> From<LanguageType> for Cow<'a, LanguageType> {
fn from(from: LanguageType) -> Self {
Cow::Owned(from)
}
}
impl<'a> From<&'a LanguageType> for Cow<'a, LanguageType> {
fn from(from: &'a LanguageType) -> Self {
Cow::Borrowed(from)
}
}

View file

@ -0,0 +1,235 @@
// Copyright (c) 2015 Aaron Power
// Use of this source code is governed by the APACHE2.0/MIT licence that can be
// found in the LICENCE-{APACHE/MIT} file.
use std::borrow::Cow;
use std::fmt;
use std::path::Path;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::collections::BTreeMap;
use utils::fs;
use self::LanguageType::*;
use Languages;
use Language;
#[cfg_attr(feature = "io", derive(Deserialize, Serialize))]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum LanguageType {
{{~#each languages}}
{{~@key}},
{{/each}}
}
impl LanguageType {
/// Returns the display name of a language.
///
/// ```
/// # use tokei::*;
/// let bash = LanguageType::Bash;
///
/// assert_eq!(bash.name(), "BASH");
/// ```
pub fn name(&self) -> &'static str {
match *self {
{{~#each languages}}
{{@key}} =>
{{#if this.name}}
"{{~name}}"
{{else}}
"{{~@key}}"
{{~/if}},
{{~/each}}
}
}
/// Get language from it's file extension.
///
/// ```no_run
/// # use tokei::*;
/// let rust = LanguageType::from_extension("./main.rs");
///
/// assert_eq!(rust, Some(LanguageType::Rust));
/// ```
pub fn from_extension<P: AsRef<Path>>(entry: P) -> Option<Self> {
if let Some(extension) = fs::get_extension(entry) {
match &*extension {
{{~#each languages}}
{{~#each this.extensions}}
"{{~this}}" {{~#unless @last}} | {{~/unless}}
{{~/each}}
=> Some({{~@key}}),
{{~/each}}
extension => {
warn!("Unknown extension: {}", extension);
None
},
}
} else {
None
}
}
}
impl Languages {
#[inline]
pub fn generate_languages() -> BTreeMap<LanguageType, Language> {
btreemap! {
{{~#each languages}}
{{~@key}} =>
{{~#if this.base}}
Language::new_{{this.base}}()
{{else}}
{{~#if this.single}}
{{~#if this.multi}}
Language::new(
vec![
{{~#each this.single}}
"{{this}}",
{{~/each}}
],
vec![
{{~#each this.multi}}
(
{{~#each this}}
"{{this}}",
{{~/each}}
),
{{~/each}}
]
)
{{else}}
Language::new_single(vec![
{{~#each this.single}}
"{{~this}}",
{{~/each}}
])
{{~/if}}
{{else}}
Language::new_multi(vec![
{{~#each this.multi}}
(
{{~#each this}}
"{{~this}}",
{{~/each}}
),
{{~/each}}
])
{{~/if}}
{{~/if}}
{{~#if this.nested}}
.nested()
{{~/if}}
{{~#if this.nested_comments}}
.nested_comments(vec![
{{~#each this.nested_comments}}
(
{{~#each this}}
"{{this}}",
{{~/each}}
),
{{~/each}}
])
{{~/if}}
{{~#if this.quotes}}
.set_quotes(vec![
{{~#each this.quotes}}
(
{{~#each this}}
"{{this}}",
{{~/each}}
),
{{~/each}}
])
{{~/if}},
{{~/each}}
}
}
}
impl From<String> for LanguageType {
fn from(from: String) -> Self {
LanguageType::from(&*from)
}
}
impl<'a> From<&'a str> for LanguageType {
fn from(from: &str) -> Self {
match &*from {
{{~#each languages}}
{{~#if this.name}}
"{{~this.name}}"
{{else}}
"{{~@key}}"
{{~/if}}
=> {{~@key}},
{{~/each}}
_ => unreachable!(),
}
}
}
impl fmt::Display for LanguageType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl<'a> From<LanguageType> for Cow<'a, LanguageType> {
fn from(from: LanguageType) -> Self {
Cow::Owned(from)
}
}
impl<'a> From<&'a LanguageType> for Cow<'a, LanguageType> {
fn from(from: &'a LanguageType) -> Self {
Cow::Borrowed(from)
}
}
/// This is for getting the file type from the first line of a file
pub fn get_filetype_from_shebang<P: AsRef<Path>>(file: P) -> Option<&'static str> {
let file = match File::open(file) {
Ok(file) => file,
_ => return None,
};
let mut buf = BufReader::new(file);
let mut line = String::new();
let _ = buf.read_line(&mut line);
let mut words = line.split_whitespace();
match words.next() {
Some("#!/bin/sh") => Some("sh"),
Some("#!/bin/csh") => Some("csh"),
Some("#!/usr/bin/perl") => Some("pl"),
Some("#!/usr/bin/env") => {
if let Some(word) = words.next() {
match word {
{{~#each languages}}
{{~#if this.env}}
{{~#each this.env}}
"{{~this}}"
{{~#unless @last}}
|
{{~/unless}}
{{~/each}}
=> Some("{{this.extensions[0]}}"),
{{~/if}}
{{~/each}}
env => {
warn!("Unknown environment: {:?}", env);
None
}
}
} else {
None
}
}
_ => None,
}
}

View file

@ -252,139 +252,7 @@ impl Languages {
/// let languages = Languages::new();
/// ```
pub fn new() -> Self {
use super::LanguageType::*;
let map = btreemap! {
ActionScript => Language::new_c(),
Ada => Language::new_single(vec!["--"]),
Assembly => Language::new_single(vec![";"])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Asp => Language::new_single(vec!["'", "REM"]),
AspNet => Language::new_multi(vec![("<!--", "-->"), ("<%--", "-->")]),
Autoconf => Language::new_single(vec!["#", "dnl"]),
Bash => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Batch => Language::new_single(vec!["REM", "::"]).set_quotes(vec![]),
C => Language::new_c(),
CHeader => Language::new_c(),
Clojure => Language::new_single(vec![";"]),
CoffeeScript => Language::new(vec!["#"], vec![("###", "###")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
ColdFusion => Language::new_multi(vec![("<!---", "--->")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
ColdFusionScript => Language::new_c(),
Coq => Language::new_func(),
Cpp => Language::new_c(),
CppHeader => Language::new_c(),
CSharp => Language::new_c(),
CShell => Language::new_hash(),
Css => Language::new_c()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
D => Language::new(vec!["//"], vec![("/*", "*/")])
.nested_comments(vec![("/+", "+/")])
.set_quotes(vec![("\"", "\""), ("`", "`")]),
Dart => Language::new_c()
.set_quotes(vec![("\"", "\""), ("'", "'"), ("\"\"\"", "\"\"\""), ("'''", "'''")]),
DeviceTree => Language::new_c(),
Elm => Language::new(vec!["--"], vec![("{-", "-}")]).nested(),
Erlang => Language::new_single(vec!["%"]),
Forth => Language::new(vec!["\\"], vec![("( ", ")")])
.set_quotes(vec![("\"", "\"")]),
FortranLegacy => Language::new_single(vec!["c","C","!","*"])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
FortranModern => Language::new_single(vec!["!"])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Glsl => Language::new_c(),
Go => Language::new_c(),
Handlebars => Language::new_multi(vec![("<!--", "-->"), ("{{!", "}}")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Haskell => Language::new(vec!["--"], vec![("{-", "-}")]).nested(),
Html => Language::new_html()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Hex => Language::new_blank(),
Idris => Language::new(vec!["--"], vec![("{-", "-}")]).nested()
.set_quotes(vec![("\"", "\""), ("\"\"\"", "\"\"\"")]),
IntelHex => Language::new_blank(),
Isabelle => Language::new(
vec!["--"],
vec![ ("{*","*}"),
("(*","*)"),
("",""),
("\\<open>", "\\<close>"),
]
).set_quotes(vec![("''", "''")]),
Jai => Language::new_c().nested(),
Java => Language::new_c(),
JavaScript => Language::new_c()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Json => Language::new_blank(),
Jsx => Language::new_c()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Julia => Language::new(vec!["#"], vec![("#=", "=#")]).nested()
.set_quotes(vec![("\"", "\""), ("\"\"\"", "\"\"\"")]),
Kotlin => Language::new_c().nested()
.set_quotes(vec![("\"", "\""), ("\"\"\"", "\"\"\"")]),
Lean => Language::new(vec!["--"], vec![("/-", "-/")]).nested(),
Less => Language::new_c(),
LinkerScript => Language::new_c(),
Lisp => Language::new(vec![";"], vec![("#|", "|#")]).nested(),
Lua => Language::new(vec!["--"], vec![("--[[", "]]")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Makefile => Language::new_hash(),
Markdown => Language::new_blank(),
Mustache => Language::new_multi(vec![("{{!", "}}")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Nim => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("\"\"\"", "\"\"\"")]),
ObjectiveC => Language::new_c(),
ObjectiveCpp => Language::new_c(),
OCaml => Language::new_func(),
Oz => Language::new_pro(),
Pascal => Language::new_multi(
vec![("{", "}"), ("(*", "*)"), ("{", "*)"), ("(*", "}")])
.set_quotes(vec![("'", "'")]),
Perl => Language::new(vec!["#"], vec![("=pod", "=cut")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Php => Language::new(vec!["#", "//"], vec![("/*", "*/")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Polly => Language::new_html(),
Prolog => Language::new_pro(),
Protobuf => Language::new_single(vec!["//"]),
Python => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("'", "'"), ("\"\"\"", "\"\"\""), ("'''", "'''")]),
Qcl => Language::new_c(),
R => Language::new_hash(),
Razor => Language::new_multi(vec![("<!--", "-->"), ("@*", "*@")]),
Ruby => Language::new(vec!["#"], vec![("=begin", "=end")])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
RubyHtml => Language::new_html(),
Rust => Language::new_c().nested(),
ReStructuredText => Language::new_blank(),
Sass => Language::new_c()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Scala => Language::new_c(),
Sml => Language::new_func(),
Sql => Language::new(vec!["--"], vec![("/*", "*/")])
.set_quotes(vec![("'", "'")]),
Swift => Language::new_c().nested(),
Tcl => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Tex => Language::new_single(vec!["%"]),
Text => Language::new_blank(),
Toml => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("'", "'"), ("\"\"\"", "\"\"\""), ("'''", "'''")]),
TypeScript => Language::new_c()
.set_quotes(vec![("\"", "\""), ("'", "'"), ("`", "`")]),
UnrealScript => Language::new_c(),
VimScript => Language::new_single(vec!["\""])
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Wolfram => Language::new_func(),
Xml => Language::new_html(),
Yaml => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
Zsh => Language::new_hash()
.set_quotes(vec![("\"", "\""), ("'", "'")]),
};
let map = Self::generate_languages();
Languages { inner: map }
}

1205
src/lib/languages.json Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,8 +4,6 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::io::{BufRead, BufReader};
use std::fs::File;
use std::path::Path;
use glob::glob;
@ -13,6 +11,7 @@ use walkdir::{WalkDir, WalkDirIterator};
use language::{Language, LanguageType};
use language::LanguageType::*;
pub use language::get_filetype_from_shebang;
macro_rules! get_language {
($languages:expr, $entry:expr) => {
@ -99,31 +98,3 @@ pub fn get_extension<P: AsRef<Path>>(path: P) -> Option<String> {
}
}
/// This is for getting the file type from the first line of a file
pub fn get_filetype_from_shebang<P: AsRef<Path>>(file: P) -> Option<&'static str> {
let file = match File::open(file) {
Ok(file) => file,
_ => return None,
};
let mut buf = BufReader::new(file);
let mut line = String::new();
let _ = buf.read_line(&mut line);
let mut words = line.split_whitespace();
match words.next() {
Some("#!/bin/sh") => Some("sh"),
Some("#!/bin/csh") => Some("csh"),
Some("#!/usr/bin/perl") => Some("pl"),
Some("#!/usr/bin/env") => {
match words.next() {
Some("python") | Some("python2") | Some("python3") => Some("py"),
Some("sh") => Some("sh"),
env => {
warn!("Unknown environment: {:?}", env);
None
}
}
}
_ => None,
}
}

View file

@ -105,11 +105,11 @@ fn main() {
if print_animation {
print!(" Counting files. \r");
thread::sleep(Duration::from_millis(4));
thread::sleep(Duration::from_millis(10));
print!(" Counting files..\r");
thread::sleep(Duration::from_millis(4));
thread::sleep(Duration::from_millis(10));
print!(" Counting files...\r");
thread::sleep(Duration::from_millis(4));
thread::sleep(Duration::from_millis(10));
}
}
});