From e2b732827553322c81d6f38d729cb7b0e27cbc80 Mon Sep 17 00:00:00 2001 From: Aaron Power Date: Tue, 30 Aug 2016 17:54:45 +0100 Subject: [PATCH] add hex ihex rst --- Cargo.lock | 88 +++++++++++++++++-------------- src/lib/language/language_type.rs | 15 ++++++ src/lib/language/languages.rs | 32 ++++------- src/lib/utils/multi_line.rs | 19 +++---- 4 files changed, 79 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ba90e9..c27aaa8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,14 +2,14 @@ name = "tokei" version = "4.2.0" dependencies = [ - "clap 2.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.10.4 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "maplit 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 0.4.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.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.4 (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.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -23,10 +23,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "aster" -version = "0.24.0" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "syntex_syntax 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -46,14 +46,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "clap" -version = "2.10.1" +version = "2.10.4" 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.15 (registry+https://github.com/rust-lang/crates.io-index)", - "strsim 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "term_size 0.1.0 (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)", @@ -121,22 +122,22 @@ dependencies = [ [[package]] name = "quasi" -version = "0.17.0" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "syntex_errors 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_syntax 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "quasi_codegen" -version = "0.17.0" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aster 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_errors 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_syntax 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aster 0.25.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.42.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -169,7 +170,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -183,24 +184,24 @@ dependencies = [ [[package]] name = "serde_codegen" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aster 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)", - "quasi 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", - "quasi_codegen 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_codegen_internals 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_syntax 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "aster 0.25.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi_codegen 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen_internals 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.42.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_codegen_internals" -version = "0.6.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "syntex_errors 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_syntax 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -211,7 +212,7 @@ 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.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -219,40 +220,40 @@ name = "serde_yaml" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", "yaml-rust 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "strsim" -version = "0.4.1" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "syntex" -version = "0.41.0" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "syntex_errors 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_syntax 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syntex_errors" -version = "0.41.0" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.15 (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.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_pos 0.42.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.41.0" +version = "0.42.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)", @@ -260,15 +261,15 @@ dependencies = [ [[package]] name = "syntex_syntax" -version = "0.41.0" +version = "0.42.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.15 (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.41.0 (registry+https://github.com/rust-lang/crates.io-index)", - "syntex_pos 0.41.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_pos 0.42.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)", ] @@ -284,7 +285,7 @@ dependencies = [ [[package]] name = "term_size" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", @@ -295,9 +296,14 @@ name = "toml" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-segmentation" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-width" version = "0.1.3" diff --git a/src/lib/language/language_type.rs b/src/lib/language/language_type.rs index b57bf70..cd8da62 100644 --- a/src/lib/language/language_type.rs +++ b/src/lib/language/language_type.rs @@ -75,8 +75,12 @@ pub enum LanguageType { Haskell, /// Html Html, + /// HEX + Hex, /// Idris Idris, + /// Intel HEX + IntelHex, /// Isabelle Isabelle, /// Jai @@ -143,6 +147,8 @@ pub enum LanguageType { RubyHtml, /// Rust Rust, + /// ReStructuredText + ReStructuredText, /// Sass Sass, /// Scala @@ -217,7 +223,9 @@ impl LanguageType { Handlebars => "Handlebars", Haskell => "Haskell", Html => "HTML", + Hex => "HEX", Idris => "Idris", + IntelHex => "Intel HEX", Isabelle => "Isabelle", Jai => "JAI", Java => "Java", @@ -251,6 +259,7 @@ impl LanguageType { Ruby => "Ruby", RubyHtml => "Ruby HTML", Rust => "Rust", + ReStructuredText => "ReStructuredText", Sass => "Sass", Scala => "Scala", Sml => "Standard ML", @@ -311,7 +320,9 @@ impl LanguageType { "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), @@ -345,6 +356,7 @@ impl LanguageType { "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), @@ -409,6 +421,8 @@ impl<'a> From<&'a str> for LanguageType { "Handlebars" => Handlebars, "Haskell" => Haskell, "Html" => Html, + "HEX" => Hex, + "Intel HEX" => IntelHex, "Idris" => Idris, "Jai" => Jai, "Java" => Java, @@ -442,6 +456,7 @@ impl<'a> From<&'a str> for LanguageType { "Ruby" => Ruby, "RubyHtml" => RubyHtml, "Rust" => Rust, + "ReStructuredText" => ReStructuredText, "Sass" => Sass, "Scala" => Scala, "Sml" => Sml, diff --git a/src/lib/language/languages.rs b/src/lib/language/languages.rs index 7c8adb8..f18a4b8 100644 --- a/src/lib/language/languages.rs +++ b/src/lib/language/languages.rs @@ -44,32 +44,35 @@ fn count_files(language_tuple: &mut (&LanguageType, &mut Language)) { let files: Vec<_> = language.files.drain(..).collect(); let mut contents = String::new(); let mut stack = vec![]; - let mut quote = None; + let mut quote; for file in files { let mut stats = Stats::new(opt_or_cont!(file.to_str())); stack.clear(); contents.clear(); + quote = None; rs_or_cont!(rs_or_cont!(File::open(file)).read_to_string(&mut contents)); - + let lines = contents.lines(); if language.is_blank() { stats.code += lines.count(); + **language += stats; continue; } 'line: for line in lines { stats.lines += 1; + let no_stack = stack.is_empty(); // FORTRAN has a rule where it only counts as a comment if it's the first // character in the column, so removing starting whitespace could cause a // miscount. let line = if is_fortran { line } else { - line.trim() + line.trim_left() }; if line.trim().is_empty() { @@ -84,30 +87,14 @@ fn count_files(language_tuple: &mut (&LanguageType, &mut Language)) { } } - let mut started_with = false; - if quote.is_none() { - let chain = language.multi_line.iter().chain(language.nested_comments.iter()); - - for &(start, _) in chain { - if line.starts_with(start) { - started_with = true; - } - } - } - multi_line::handle_multi_line(line, &language, &mut stack, &mut quote); - if !stack.is_empty() { - if started_with { - stats.code += 1; - } else { - stats.comments += 1; - } + if no_stack { + stats.comments += 1; } else { stats.code += 1; } } - **language += stats; } } @@ -300,7 +287,9 @@ impl Languages { Haskell => Language::new_single(vec!["--"]), Html => Language::new_html() .set_quotes(vec![("\"", "\""), ("'", "'")]), + Hex => Language::new_blank(), Idris => Language::new(vec!["--"], vec![("{-", "-}")]), + IntelHex => Language::new_blank(), Isabelle => Language::new( vec!["--"], vec![ ("{*","*}"), @@ -343,6 +332,7 @@ impl Languages { Ruby => Language::new(vec!["#"], vec![("=begin", "=end")]), RubyHtml => Language::new_html(), Rust => Language::new_c().nested(), + ReStructuredText => Language::new_blank(), Sass => Language::new_c(), Scala => Language::new_c(), Sml => Language::new_func(), diff --git a/src/lib/utils/multi_line.rs b/src/lib/utils/multi_line.rs index 1c3c300..81b7cbc 100644 --- a/src/lib/utils/multi_line.rs +++ b/src/lib/utils/multi_line.rs @@ -6,7 +6,6 @@ pub fn handle_multi_line(line: &str, stack: &mut Vec<&'static str>, quote: &mut Option<&'static str>) { let mut chars = line.chars(); - let mut cont = false; let nested_is_empty = language.nested_comments.is_empty(); 'window: loop { @@ -16,17 +15,11 @@ pub fn handle_multi_line(line: &str, } chars.next(); - // Prevents counting overlaps like /*/* - if cont { - cont = false; - continue; - } - let mut end = false; if let &mut Some(quote_str) = quote { if window.starts_with("\\") { - cont = true; + chars.next(); continue; } else if window.starts_with(quote_str) { end = true; @@ -38,7 +31,7 @@ pub fn handle_multi_line(line: &str, *quote = None; if quote_str.chars().count() == 1 { - cont = true + chars.next(); } continue; } @@ -57,7 +50,7 @@ pub fn handle_multi_line(line: &str, if pop { stack.pop(); - cont = true; + chars.next(); continue; } @@ -66,7 +59,7 @@ pub fn handle_multi_line(line: &str, for &(start, end) in &language.quotes { if window.starts_with(start) { *quote = Some(end); - cont = true; + chars.next(); continue 'window; } } @@ -82,7 +75,7 @@ pub fn handle_multi_line(line: &str, for &(start, end) in &language.nested_comments { if window.starts_with(start) { stack.push(end); - cont = true; + chars.next(); continue 'window; } } @@ -94,7 +87,7 @@ pub fn handle_multi_line(line: &str, } else if stack.len() == 0 { stack.push(end); } - cont = true; + chars.next(); continue 'window; } }