github.com/aretext/aretext@v1.3.0/syntax/syntax.go (about)

     1  package syntax
     2  
     3  import (
     4  	"github.com/aretext/aretext/syntax/languages"
     5  	"github.com/aretext/aretext/syntax/parser"
     6  )
     7  
     8  // Language is an enum of languages that we can parse.
     9  type Language string
    10  
    11  // AllLanguages lists every available language.
    12  var AllLanguages []Language
    13  
    14  const (
    15  	LanguagePlaintext    = Language("plaintext")
    16  	LanguageJson         = Language("json")
    17  	LanguageYaml         = Language("yaml")
    18  	LanguageGo           = Language("go")
    19  	LanguageGoTemplate   = Language("gotemplate")
    20  	LanguagePython       = Language("python")
    21  	LanguageRust         = Language("rust")
    22  	LanguageC            = Language("c")
    23  	LanguageBash         = Language("bash")
    24  	LanguageXml          = Language("xml")
    25  	LanguageGitCommit    = Language("gitcommit")
    26  	LanguageGitRebase    = Language("gitrebase")
    27  	LanguageProtobuf     = Language("protobuf")
    28  	LanguageTodoTxt      = Language("todotxt")
    29  	LanguageMarkdown     = Language("markdown")
    30  	LanguageCriticMarkup = Language("criticmarkup")
    31  	LanguageMakefile     = Language("makefile")
    32  	LanguageP4           = Language("p4")
    33  )
    34  
    35  // languageToParseFunc maps each language to its parse func.
    36  var languageToParseFunc map[Language]parser.Func
    37  
    38  func init() {
    39  	languageToParseFunc = map[Language]parser.Func{
    40  		LanguagePlaintext:    nil,
    41  		LanguageJson:         languages.JsonParseFunc(),
    42  		LanguageYaml:         languages.YamlParseFunc(),
    43  		LanguageGo:           languages.GolangParseFunc(),
    44  		LanguageGoTemplate:   languages.GoTemplateParseFunc(),
    45  		LanguagePython:       languages.PythonParseFunc(),
    46  		LanguageRust:         languages.RustParseFunc(),
    47  		LanguageC:            languages.CParseFunc(),
    48  		LanguageBash:         languages.BashParseFunc(),
    49  		LanguageXml:          languages.XmlParseFunc(),
    50  		LanguageGitCommit:    languages.GitCommitParseFunc(),
    51  		LanguageGitRebase:    languages.GitRebaseParseFunc(),
    52  		LanguageProtobuf:     languages.ProtobufParseFunc(),
    53  		LanguageTodoTxt:      languages.TodoTxtParseFunc(),
    54  		LanguageMarkdown:     languages.MarkdownParseFunc(),
    55  		LanguageCriticMarkup: languages.CriticMarkupParseFunc(),
    56  		LanguageMakefile:     languages.MakefileParseFunc(),
    57  		LanguageP4:           languages.P4ParseFunc(),
    58  	}
    59  
    60  	for language := range languageToParseFunc {
    61  		AllLanguages = append(AllLanguages, language)
    62  	}
    63  }
    64  
    65  // ParseForLanguage creates a parser for a syntax language.
    66  // If no parser is available (e.g. for LanguagePlaintext) this returns nil.
    67  func ParserForLanguage(language Language) *parser.P {
    68  	parseFunc := languageToParseFunc[language]
    69  	if parseFunc == nil {
    70  		return nil
    71  	}
    72  	return parser.New(parseFunc)
    73  }