From 2016c43e93629ca2484f74ec3dbb2efbee297222 Mon Sep 17 00:00:00 2001 From: Ian Lewis Date: Fri, 25 Aug 2023 09:35:33 +0900 Subject: [PATCH] feat: Add R support (#332) Updates #1 Signed-off-by: Ian Lewis --- CHANGELOG.md | 2 +- internal/scanner/config.go | 11 +++++ internal/scanner/scanner_test.go | 77 ++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db9bda0..ef990c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Support for Erlang, SQL, and Haskell programming languages has been added. +- Support for Erlang, Haskell, R, and SQL programming languages has been added. ## [0.4.0] - 2023-08-23 diff --git a/internal/scanner/config.go b/internal/scanner/config.go index 11b55eb..a1a3d8d 100644 --- a/internal/scanner/config.go +++ b/internal/scanner/config.go @@ -214,6 +214,17 @@ var ( escapeFunc: backslashEscape, } + // RConfig is a config for R. + RConfig = Config{ + LineCommentStart: []string{"#"}, + // NOTE: R has no multi-line comments. + Strings: [][2]string{ + {"\"", "\""}, + {"'", "'"}, + }, + escapeFunc: backslashEscape, + } + // RubyConfig is a config for Ruby. RubyConfig = Config{ LineCommentStart: []string{"#"}, diff --git a/internal/scanner/scanner_test.go b/internal/scanner/scanner_test.go index 789c304..09c21b4 100644 --- a/internal/scanner/scanner_test.go +++ b/internal/scanner/scanner_test.go @@ -720,6 +720,83 @@ var scannerTestCases = []*struct { }, }, + // R + { + name: "line_comments.r", + src: `# file comment + + # TODO is a function + TODO <- function() { + print("Hello World") # Random comment + `, + config: RConfig, + comments: []struct { + text string + line int + }{ + { + text: "# file comment", + line: 1, + }, + { + text: "# TODO is a function", + line: 3, + }, + { + text: "# Random comment", + line: 5, + }, + }, + }, + { + name: "comments_in_string.r", + src: `# file comment + + # TODO is a function + TODO <- function() { + print("# Random comment") + print('# Random comment') + `, + config: RConfig, + comments: []struct { + text string + line int + }{ + { + text: "# file comment", + line: 1, + }, + { + text: "# TODO is a function", + line: 3, + }, + }, + }, + { + name: "escaped_string.r", + src: `# file comment + + # TODO is a function + TODO <- function() { + print("\"# Random comment") + print('\'# Random comment') + `, + config: RConfig, + comments: []struct { + text string + line int + }{ + { + text: "# file comment", + line: 1, + }, + { + text: "# TODO is a function", + line: 3, + }, + }, + }, + // Ruby { name: "raw_string.rb",