Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CSV Schema language #1039

Merged
merged 13 commits into from
Sep 17, 2019
Next Next commit
Added CSV Schema language
  • Loading branch information
filipegarcia authored and pyrmont committed Sep 2, 2019
commit 9ea57bafbbcc75bdab0960efd5eaa292eb669ea1
8 changes: 8 additions & 0 deletions lib/rouge/demos/csvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version 1.1
@totalColumns 5
@separator ','
Transaction_Date: xDate
Transaction_ID: notEmpty
Originator_Name: notEmpty
Originator_Address: any("yes","no")
Originator_Country: notEmpty
69 changes: 69 additions & 0 deletions lib/rouge/lexers/csvs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class CSVS < RegexLexer
tag 'csvs'
title "csvs"
desc 'The CSV Schema Language (http://digital-preservation.github.io/csv-schema/)'
mimetypes 'text/x-csvssrc'
filenames '*.csvs'

def self.detect?(text)
return true if text.shebang? 'csvs'
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
end

def self.constants
@constants ||= Set.new %w(nil false true)
end

def self.builtins
@builtins ||= Set.new %w(
args call clone do doFile doString else elseif for if list
method return super then
)
end

state :root do
rule /\s+/m, Text
rule %r(//.*?\n), Comment::Single
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule %r(#.*?\n), Comment::Single
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule %r(/(\\\n)?[*].*?[*](\\\n)?/)m, Comment::Multiline
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule %r(/[+]), Comment::Multiline, :nested_comment

rule /"(\\\\|\\"|[^"])*"/, Str

rule %r(:?:=), Keyword
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule /[()]/, Punctuation

rule %r([-=;,*+><!/|^.%&\[\]{}]), Operator
pyrmont marked this conversation as resolved.
Show resolved Hide resolved

rule /[A-Z]\w*/, Name::Class

rule /[a-z_]\w*/ do |m|
name = m[0]
pyrmont marked this conversation as resolved.
Show resolved Hide resolved

if self.class.constants.include? name
token Keyword::Constant
elsif self.class.builtins.include? name
token Name::Builtin
else
token Name
end
end

rule %r((\d+[.]?\d*|\d*[.]\d+)(e[+-]?[0-9]+)?)i, Num::Float
rule /\d+/, Num::Integer

rule /@@?|\'|\:/, Keyword
end

state :nested_comment do
rule %r([^/+]+)m, Comment::Multiline
rule %r(/[+]), Comment::Multiline, :nested_comment
rule %r([+]/), Comment::Multiline, :pop!
end
end
end
end
22 changes: 22 additions & 0 deletions spec/lexers/csvs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

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

describe 'guessing' do
include Support::Guessing

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

it 'guesses by mimetype' do
assert_guess :mimetype => 'text/x-csvssrc'
end

it 'guesses by source' do
assert_guess :source => '#!/usr/local/bin/csvs'
end
end
end
22 changes: 22 additions & 0 deletions spec/visual/samples/csvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version 1.0
@totalColumns 19
@separator ','
Customer_Name: notEmpty
Customer_Date_of_Birth: xDate
Customer_Place_of_Birth: notEmpty
Customer_Address: notEmpty
Customer_Account_number: notEmpty
Customer_Legal_Entity_Type: notEmpty
Customer_Industry: notEmpty
Adverse_Information_Search: xDate
Customer_Product_Usage: notEmpty
Customer_Net_Worth: notEmpty
Customer_Cash_Balance: notEmpty
Account_Open_Date: notEmpty
CDD_Completion_Date: notEmpty
System_Customer_Risk_Rating: notEmpty
PEP_Status: any("yes","no")
Associated_PEP: any("yes","no")
EDD_Triggered: any("yes","no")
Last_CDD_Renewal_Date: xDate
Relationship_Manager_Employee_ID: notEmpty