Giter VIP home page Giter VIP logo

dialect's Introduction

Dialect Build Status

Dialect is a framework for processing source code to determine basis linguistic details such as number of lines of comments, etc.

Dialect implements a language specific set of rules to determine these details.

If Dialect cannot determine the language or doesn't have a specific implementation, it will simply return the number of lines only.

Installation

go get github.com/pinpt/dialect

Usage

The most simple usage is to invoke Examine passing it the language, filename and a io.Reader to the source code.

package main

import (
	"fmt"
	"github.com/pinpt/dialect"
	"strings"
)

func main() {
	reader := strings.NewReader("var a = 1")
	config := dialect.CreateDefaultConfiguration()
	result, err := dialect.Examine("JavaScript", "test.js", reader, config)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}

The result is a struct DialectResult which provides the details of what the examiner found.

type DialectResult struct {
	Blanks     int
	Comments   int
	Loc        int
	Sloc       int
	IsTest     bool
	Copyrights []*copyright.CopyrightResult
}

The fields are the following:

  • Blanks the number of blank lines detected
  • Comments the number of comment lines detected
  • Loc the total number of lines detected (Blanks + Comments + Sloc = Loc)
  • Sloc the number of source lines detected (Sloc = Loc - Comments - Blanks)
  • IsTest a boolean to indicate if the examiner detected the file to be a test file
  • Copyrights an array of copyrights found in the file (only available if DetectCopyrights is set in config)

Detecting line by line

Instead of using a Reader, you can feed data line by line.

config := dialect.CreateDefaultConfiguration()
ex, err := dialect.CreateLineByLineExaminer("JavaScript", "foo.js", config)
if err != nil {
	panic(err)
}
// pass true as 2nd argument to indicate the EOF
_, err = ex.ProcessLine("var a = 1", true)
if err != nil {
	panic(err)
}
// results will be in ex.Results

Detecting Copyrights

You can detect Copyrights in source code by setting the configuration field DetectCopyrights to true.

config := dialect.CreateDefaultConfiguration()
config.DetectCopyrights = true

If any copyrights are detected in the source code, the Copyrights field will be an array of *CopyrightResult. If none are found, the Copyrights will be nil.

Implementing a missing language

To implement your own language or override an existing language, you can use the function RegisterExaminer and implement the interface DialectExaminer. See any of the existing implementations for a good example.

If you do implement a new language or fix an existing one, please consider sending us a Pull Request so that we can merge it!

License

Licensed under the MIT License. Copyright (c) 2016-2017 PinPT, Inc.

dialect's People

Contributors

jhaynie avatar robindiddams avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.