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

Add marker and terminal for compile errors #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/complie_samples/ca.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from subprocess import Popen, PIPE, STDOUT, TimeoutExpired

import util
from cga import TokenNode
from parser import InfoBlock, CodeBlock


def gcc(code: str) -> str:
filename = util.write2file(code, '.c')

cmd = ['gcc', '-g',
# '-fdiagnostics-format=json',
'-fdiagnostics-parseable-fixits',
'-Werror=implicit-function-declaration',
filename,
'-o', filename + '.out',
]
proc = Popen(" ".join(cmd), stdout=PIPE, stderr=STDOUT, shell=True)
try:
outs, errs = proc.communicate(timeout=30)
assert isinstance(outs, bytes)
outs = outs.decode('utf-8')
except TimeoutExpired:
proc.kill()
proc.communicate()
outs = 'The task is killed because of timeout'
return outs


def build_block(error_info: str, block_type=InfoBlock) -> CodeBlock:
assert isinstance(error_info, str)
block = block_type()
for line in error_info.split('\n'):
block.append(TokenNode({'text_line': line}))
return block
126 changes: 126 additions & 0 deletions docs/complie_samples/cga.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import logging
from typing import List, Dict

from util import dict2ins, link_node


class LexSta:
START = 1
PROCESSING = START << 1
END = START << 2


class StaGraph(object):

def __init__(self):
self.__stat__ = LexSta.START

def evolve(self, sta):
next = self.__stat__ << 1 & 7

if not next:
next = LexSta.START

if sta ^ next:
return False
else:
self.__stat__ = next
return True


class Buffer(object):

def __init__(self, line: str):
assert line[-1] == '\n'
self.__buf_ = line
self.__nxt_ = 0
self.__cur_ = 0

def next(self):
val = self.__buf_[self.__nxt_]
self.__cur_ = self.__nxt_
self.__nxt_ += 1
return val

def current(self):
return self.__buf_[self.__cur_]

def seek(self, offset):
return self.__buf_[self.__cur_ + offset]

def seeks(self, offset):
if offset > 0:
return self.__buf_[self.__cur_ + 1: self.__cur_ + 1 + offset]
elif offset < 0:
return self.__buf_[self.__cur_ + offset: self.__cur_]
else:
return self.current()

@property
def buf_str(self) -> str:
return self.__buf_

@buf_str.setter
def buf_str(self, value):
self.__buf_ = value


class TokenNode(object):

def __init__(self, data: dict):
self.__data = data
self.__next = None

def get(self, key: str):
return self.__data.get(key)

@property
def next(self):
return self.__next

@next.setter
def next(self, node):
self.__next = node

@property
def key(self):
return list(self.__data.keys())[0]

@property
def val(self):
return self.get(self.key)

def __eq__(self, other):
logging.info('self key:' + self.key)
logging.info('other key:' + other.key)
logging.info('self val:' + self.val)
logging.info('other val:' + other.val)

if not other:
return False

if self is other:
return True

if self.key != other.key:
return False

if self.val != other.val:
return False

if self.next != other.next:
return False

return True

def __str__(self) -> str:
return str(self.__data)


@link_node
def make_token_list(tokens: List[Dict[str, str]]) -> List[TokenNode]:
nodelist = []
for token in tokens:
d = {'data': token}
nodelist.append(dict2ins(d, TokenNode))
return nodelist
86 changes: 86 additions & 0 deletions docs/complie_samples/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# The technical architecture of cga

## flow

```mermaid
flowchart TB

subgraph codeblock
_linked_list_node_4[code_start]
_linked_list_node_4 --> _linked_list_node_5[text_line]
_linked_list_node_5 --> _linked_list_node_6[code_end]
end

subgraph tokenlist
_linked_list_header[h1] --> _linked_list_node_0[text_line]
_linked_list_node_0 --> _linked_list_node_1[text_line]
_linked_list_node_1 --> _linked_list_node_2[h2]
_linked_list_node_2 --> _linked_list_node_3[text_line]
_linked_list_node_3 --> codeblock
codeblock x-. insert .-x _linked_list_node_7[h2]
_linked_list_node_7 --> _linked_list_node_8[text_line]
end

subgraph lexer
_lexer[lex]
end

subgraph parser
_blocks_hunter[hunt]
_insert[insert]
end

subgraph gener
_generate[todoc]
end

subgraph cc-accessor
_gcca[ca] -- code --> _gcc[gcc]
_gcc --error info --> _gcca

_gcca -- code --> _clang[clang]
_clang -- error info --> _gcca

_gcca -- code --> _msvc[msvc]
_msvc -- error info --> _gcca
end

subgraph errorblock
_error_info_header[h2] --> _error_info_node_1[text_line]
_error_info_node_1 --> _error_info_node_2[code_start]
_error_info_node_2 --> _error_info_node_3[text_line]
_error_info_node_3 --> _error_info_node_4[text_line]
_error_info_node_4 --> _error_info_node_5[code_end]
end


codeblock -- insert --> errorblock
errorblock -- insert --> _linked_list_node_7

codeblock ==> _blocks_hunter
_blocks_hunter == code ==> cc-accessor ==> errorblock
lexer ==> tokenlist

tokenlist ==> gener
_generate ==> _md_file[markdown]

```

```
+ Public
- Private
# Protected
~ Package/Internal
```

```json
[
{"h2": "##"},
{"text_line": "this is h2"},
{"code_start": "```c"},
{"text_line": "int main() {"},
{"text_line": " return 0;"},
{"text_line": "}"},
{"code_end": "```"}
]
```
104 changes: 104 additions & 0 deletions docs/complie_samples/discern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import re

from cga import Buffer

TITLE_LEVEL = ['h1', 'h2', 'h3', 'h4', 'h5']


def ll_title(buf: Buffer):
regex = '^#+ '
group = re.search(regex, buf.buf_str)
if group:
match = group.group(0)
level = TITLE_LEVEL[len(match) - 2]
sign = match[:-1]
return {
'matched': True,
'token': level,
'sign': sign,
'remain': buf.buf_str[len(match):]
}
return {
'matched': False,
'remain': buf.buf_str
}


def ll_text_line(buf: Buffer):
regex = '.+\n\Z'
group = re.search(regex, buf.buf_str)
if group:
match = group.group(0)
sign = match[:-1]
return {
'matched': True,
'token': 'text_line',
'sign': sign,
'remain': ''
}
return {
'matched': False,
'remain': buf.buf_str
}


def ll_code_start(buf: Buffer):
regex = '^```((c|C)(()|(\+{2})))\n\Z'
group = re.search(regex, buf.buf_str)
if group:
match = group.group(0)
sign = match[:-1]
return {
'matched': True,
'token': 'code_start',
'sign': sign,
'remain': ''
}
return {
'matched': False,
'remain': buf.buf_str
}


def ll_code_end(buf: Buffer):
regex = '^```\n'
group = re.search(regex, buf.buf_str)
if group:
match = group.group(0)
sign = match[:-1]
return {
'matched': True,
'token': 'code_end',
'sign': sign,
'remain': ''
}
return {
'matched': False,
'remain': buf.buf_str
}


PRIORITY_QUEUE = [ll_title, ll_code_start, ll_code_end, ll_text_line]


def lexer(buf: Buffer):
tokens = []
for f in PRIORITY_QUEUE:
ll = f(buf)
buf.buf_str = ll.get('remain')
if ll.get('matched'):
tokens.append({ll.get('token'): ll.get('sign')})
return tokens


def lex(file: str):
res = []
with open(file, mode='r', encoding='utf-8') as f:
while True:
line = f.readline()
if not line:
break
buffer = Buffer(line)
res.extend(lexer(buffer))
f.close()
return res
25 changes: 25 additions & 0 deletions docs/complie_samples/gener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List

import discern
from cga import TokenNode


def convert_to_inlines(nodelist: List[TokenNode]) -> List[str]:
inlines = []
node = nodelist[0]
while node:
if node.key in discern.TITLE_LEVEL:
inlines.append(node.val + ' ' + node.next.val)
node = node.next.next
continue
inlines.append(node.val)
node = node.next
return inlines


def write_to_md(nodelist: List[TokenNode], target: str) -> None:
inlines = convert_to_inlines(nodelist)
with open(target, mode='w', encoding='utf-8') as f:
for line in inlines:
f.write(line + '\n')
f.close()
Loading