Skip to content

Commit

Permalink
Add haskell support for header
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 7, 2023
1 parent 53f6fb8 commit 1d282b3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
3 changes: 3 additions & 0 deletions norma2/checkers/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
from norma2.checkers.default import check_default
from norma2.checkers.for_all import check_for_all
from norma2.checkers.makefile import check_makefile
from norma2.checkers.haskell import check_haskell
from norma2.errors.norm import _TemplateNormError
from norma2.parser.cfile import CFile
from norma2.parser.file import DefaultFile, File
from norma2.parser.hsfile import HSFile
from norma2.parser.makefile import MakeFile

choices = {
CFile: check_c.check,
DefaultFile: check_default.check,
MakeFile: check_makefile.check,
HSFile: check_haskell.check
}


Expand Down
16 changes: 16 additions & 0 deletions norma2/checkers/haskell/check_haskell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List

from norma2.checkers.haskell.header import check_header
from norma2.errors.norm import _TemplateNormError
from norma2.parser.hsfile import HSFile

checkerss = [
check_header,
]


def check(file: HSFile) -> List[_TemplateNormError]:
errs: List[_TemplateNormError] = []
for checker in checkerss:
errs.extend(checker(file))
return errs
30 changes: 30 additions & 0 deletions norma2/checkers/haskell/header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List

from norma2.errors.norm import FileHeader, _TemplateNormError
from norma2.parser.hsfile import HSFile


def check_header(file: HSFile) -> List[_TemplateNormError]:
lines = file.lines_origin[:]
if len(lines) < 6:
return [FileHeader(file.filepath, 0, "No header..")]
if lines[0] != "{-":
return [FileHeader(file.filepath, 1, "No header..")]
if not lines[1].startswith("-- EPITECH PROJECT, "):
return [
FileHeader(file.filepath, 2, "line must starts with `** EPITECH PROJECT, `")
]
if len(lines[2]) < 4 or not lines[2].startswith("-- "):
return [FileHeader(file.filepath, 3, "line must starts with `** `")]
if lines[3] != "-- File description:":
return [FileHeader(file.filepath, 4, "line must be `** File description:`")]
i = 4
while i < len(lines) and lines[i] != "-}":
if len(lines[i]) < 4 or not lines[i].startswith("-- "):
return [FileHeader(file.filepath, i + 1, "line must starts with `** `")]
i += 1
if i >= len(lines):
return [FileHeader(file.filepath, i + 1, "File with only header ?")]
if lines[i] != "-}":
return [FileHeader(file.filepath, i + 1, "line must be `*/`")]
return []
3 changes: 3 additions & 0 deletions norma2/parser/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from norma2.errors.norm import _TemplateNormError
from norma2.parser._file import _File
from norma2.parser.cfile import CFile
from norma2.parser.hsfile import HSFile
from norma2.parser.makefile import MakeFile


Expand All @@ -25,6 +26,8 @@ def __init__(self, filepath: str, config: Config):
self.file_obj = CFile(filepath, config)
elif filepath.endswith("Makefile"):
self.file_obj = MakeFile(filepath, config)
elif filepath.endswith(".hs"):
self.file_obj = HSFile(filepath, config)
else:
self.file_obj = DefaultFile(filepath, config)

Expand Down
18 changes: 18 additions & 0 deletions norma2/parser/hsfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List

from norma2.config.config_class import Config
from norma2.parser._file import _File
from norma2.errors.norm import _TemplateNormError

class HSFile(_File):
def __init__(self, filepath: str, config: Config) -> None:
super().__init__(filepath, config)

def init(self):
if not self.text_origin:
return
self.lines_origin = self.text_origin.split("\n")
self.is_init = True

def check_norm(self) -> List[_TemplateNormError]:
return []

0 comments on commit 1d282b3

Please sign in to comment.