Share
Embed
事前知識なしで理解する、静的検査のいろは / Introduction about static analysis without previous knowledge
私は Vim script の静的検査ツール「Vint」の開発者です。この発表では、Vint の開発を通じて学んだ静的検査ツールの仕組みと経験則を、事前知識のない方でも理解できるように詳解します。
この発表で触れるさまざまな基礎技術は、静的検査はもちろん、メタプログラミングやトランスパイラ、独自プログラミング言語の開発にも役立つ重要なものです。これらを技術の引き出しとして覚えておけば、今後の開発体験を豊かにしてくれることでしょう。
https://builderscon.io/tokyo/2018/session/0a3ee8c0-44ce-4cb9-9199-05b350887d79
Transcript
244 slides
-
,VOJXBL w ॴଐˠ w HJUIVCDPN,VOJXBL w RJJUBDPN,VOJXBL w ίϯϐϡʔλαΠΤϯεΛ ֶΜͰ͖͍ͯ·ͤΜ
w ࣄલࣝͳ͠Ͱ੩తղੳ πʔϧΛ։ൃ͖ͯͨ͠ -
$44ͷมߋΛՄࢹԽ͢Δπʔϧ https://github.com/mixi-inc/css-semdiff $ css-astdiff a.css b.css --verbose extra: .extra-1 {
border: none; } missing: .missing-1 { border: none; } --------------------------------- 1 extra rules and 1 missing rules -
ݕ͍ࠪͨ͠ϓϩάϥϜΛ࣮ߦͯ͠ʢಈతʣ ݕࠪ͢Δ߹ɺݕࠪதʹ௨ա͍ͯ͠ͳ͍ ίʔυݕࠪͰ͖ͳ͍ isFoo = true if isFoo { }
else { } ͪ͜ΒಈతʹݕࠪͰ͖Δ ͪ͜ΒಈతʹݕࠪͰ͖ͳ͍ -
ίʔυϨϏϡʔࣗಈԽͷྫ http://alpha.mixi.co.jp/entry/2015/12/20/000000 $ css-astdiff a.css b.css --verbose extra: .extra-1 {
border: none; } missing: .missing-1 { border: none; } --------------------------------- 1 extra rules and 1 missing rules ߦͷ$44Λ োθϩͰ-FTT Ҡߦͨ͠ࡍͷπʔϧ -
֓೦Λཧղ͢Δ நߏจΛཧղ͢Δ நߏจͷ࡞ΓํΛཧղ͢Δ நߏจͷѻ͍ํΛཧղ͢Δ ࡞ΓํΛཧղ͢Δ ؆୯ͳ੩తղੳΛ࣮͢Δ
੩తղੳͷσβΠϯύλʔϯΛΔ ࣮ࡍͷ੩తղੳΛಡΉ -
நߏจ ίʔυΛߏͰදݱͨ͠ͷ ଟ͘ͷ੩తղੳͷجຊ τʔΫϯྻ ίʔυͷখ͞ͳߏ୯ҐΛ ग़ݱॱʹฒͨͷ ελΠϧʹؔ͢Δ੩తղੳͰ Α͘ΘΕΔ ੍ޚϑϩʔάϥϑ ίʔυͷ݅ذͳͲͷ
੍ޚߏΛ࿈݁ͨ͠ϒϩοΫͰ දݱͨ͠ͷ ίʔϧάϥϑ ؔͷݺͼग़ؔ͠ΛάϥϑͰ දݱͨ͠ͷ ະ༻ؔͷղੳͳͲʹΑ͘ ΘΕΔ -
τʔΫϯྻ ίʔυͷখ͞ͳߏ୯ҐΛ ग़ݱॱʹฒͨͷ ελΠϧʹؔ͢Δ੩తղੳͰ Α͘ΘΕΔ ੍ޚϑϩʔάϥϑ ίʔυͷ݅ذͳͲͷ ੍ޚߏΛ࿈݁ͨ͠ϒϩοΫͰ දݱͨ͠ͷ ίʔϧάϥϑ
ؔͷݺͼग़ؔ͠ΛάϥϑͰ දݱͨ͠ͷ ະ༻ؔͷղੳͳͲʹΑ͘ ΘΕΔ நߏจ ίʔυΛߏͰදݱͨ͠ͷ ଟ͘ͷ੩తղੳͷجຊ ࠓճղઆ͢Δͷ -
if isEmpty('') { // COMMENT } 1SPHSBN *G4UBUFNFOU 4USJOH-JUFSBM 'VOD$BMM
*EFOUJpFS $PNNFOU நߏจ ίʔυ -
if isEmpty('') { // COMMENT } நߏจ ίʔυ ˞4UBUFNFOUʮจʯͷҙຯͳͷͰɺ*G4UBUFNFOUʮJGจʯͷҙຯͰ͢ 1SPHSBN
4USJOH-JUFSBM 'VOD$BMM *EFOUJpFS $PNNFOU *G4UBUFNFOU -
if isEmpty('') { // COMMENT } 1SPHSBN *G4UBUFNFOU 4USJOH-JUFSBM *EFOUJpFS
$PNNFOU 'VOD$BMM நߏจ ίʔυ -
if isEmpty('') { // COMMENT } 1SPHSBN *G4UBUFNFOU 4USJOH-JUFSBM 'VOD$BMM
$PNNFOU *EFOUJpFS நߏจ ίʔυ -
if isEmpty('') { // COMMENT } 1SPHSBN *G4UBUFNFOU 'VOD$BMM *EFOUJpFS
$PNNFOU 4USJOH-JUFSBM நߏจ ίʔυ -
if isEmpty('') { // COMMENT } 1SPHSBN *G4UBUFNFOU 4USJOH-JUFSBM 'VOD$BMM
*EFOUJpFS $PNNFOU நߏจ ίʔυ -
if isEmpty('') { // COMMENT } நߏจ *G4UBUFNFOU 4USJOH-JUFSBM 'VOD$BMM
*EFOUJpFS $PNNFOU 1SPHSBN ίʔυ -
if isEmpty('') { // COMMENT } 1SPHSBN *G4UBUFNFOU 4USJOH-JUFSBM 'VOD$BMM
*EFOUJpFS $PNNFOU நߏจ ίʔυ நߏจɺίʔυͷߏΛ ߏͷσʔλͱͯ͠දݱͨ͠ͷ -
1ZUIPOͷநߏจͷݟํ import ast source = "if is_empty(''):\n" \ " pass\n"
node = ast.parse(source, "sample.py") print(ast.dump(node)) -
if is_empty(''): pass நߏจ 1ZUIPO Module( body=[ If( test=Call( func=Name(
id='is_empty', ctx=Load() ), args=[ Str(s='') ], keywords=[] ), body=[ Pass() ], orelse=[] ) ] ) -
if is_empty(''): pass நߏจ 1ZUIPO Module( body=[ If( test=Call( func=Name(
id='is_empty', ctx=Load() ), args=[ Str(s='') ], keywords=[] ), body=[ Pass() ], orelse=[] ) ] ) -
if is_empty(''): pass நߏจ 1ZUIPO Module( body=[ If( test=Call( func=Name(
id='is_empty', ctx=Load() ), args=[ Str(s='') ], keywords=[] ), body=[ Pass() ], orelse=[] ) ] ) -
if is_empty(''): pass நߏจ 1ZUIPO Module( body=[ If( test=Call( func=Name(
id='is_empty', ctx=Load() ), args=[ Str(s='') ], keywords=[] ), body=[ Pass() ], orelse=[] ) ] ) -
if is_empty(''): pass நߏจ 1ZUIPO Module( body=[ If( test=Call( func=Name(
id='is_empty', ctx=Load() ), args=[ Str(s='') ], keywords=[] ), body=[ Pass() ], orelse=[] ) ] ) -
if is_empty(''): pass நߏจ 1ZUIPO Module( body=[ If( test=Call( func=Name(
id='is_empty', ctx=Load() ), args=[ Str(s='') ], keywords=[] ), body=[ Pass() ], orelse=[] ) ] ) -
3VCZͷநߏจͷݟํ require 'ripper' require 'pp' example = <<~END if is_empty('')
end END pp Ripper.sexp(example) -
நߏจ 3VCZ [:program, [ [:if, [:method_add_arg, [:fcall, [:@ident, "is_empty", [1,
3] ] ], [:arg_paren, [:args_add_block, [[:string_literal, [:string_content]]], false ] ] ], [[:void_stmt]], nil ] ] ] if is_empty('') end -
[:program, [ [:if, [:method_add_arg, [:fcall, [:@ident, "is_empty", [1, 3] ]
], [:arg_paren, [:args_add_block, [[:string_literal, [:string_content]]], false ] ] ], [[:void_stmt]], nil ] ] ] நߏจ 3VCZ if is_empty('') end -
[:program, [ [:if, [:method_add_arg, [:fcall, [:@ident, "is_empty", [1, 3] ]
], [:arg_paren, [:args_add_block, [[:string_literal, [:string_content]]], false ] ] ], [[:void_stmt]], nil ] ] ] நߏจ 3VCZ if is_empty('') end -
[:program, [ [:if, [:method_add_arg, [:fcall, [:@ident, "is_empty", [1, 3] ]
], [:arg_paren, [:args_add_block, [[:string_literal, [:string_content]]], false ] ] ], [[:void_stmt]], nil ] ] ] நߏจ 3VCZ if is_empty('') end -
[:program, [ [:if, [:method_add_arg, [:fcall, [:@ident, "is_empty", [1, 3] ]
], [:arg_paren, [:args_add_block, [[:string_literal, [:string_content]]], false ] ] ], [[:void_stmt]], nil ] ] ] நߏจ 3VCZ if is_empty('') end -
+BWB4DSJQUͷநߏจͷݟํ import {default as Esprima} from "esprima"; const program =
"if (isEmpty('')) {\n" + "}\n"; const node = Esprima.parseScript(program); console.log("%j", node); -
நߏจ +BWB4DSJQU { "type": "Program", "body": [ { "type": "IfStatement",
"test": { "type": "CallExpression", "callee": { "type": "Identifier", "name": "isEmpty" }, "arguments": [ { "type": "Literal", "value": "", "raw": "''" } ] }, "consequent": { "type": "BlockStatement", "body": [] }, "alternate": null } ], "sourceType": "script" } if (isEmpty('')) { } -
நߏจ +BWB4DSJQU { "type": "Program", "body": [ { "type": "IfStatement",
"test": { "type": "CallExpression", "callee": { "type": "Identifier", "name": "isEmpty" }, "arguments": [ { "type": "Literal", "value": "", "raw": "''" } ] }, "consequent": { "type": "BlockStatement", "body": [] }, "alternate": null } ], "sourceType": "script" } if (isEmpty('')) { } -
நߏจ +BWB4DSJQU if (isEmpty('')) { } { "type": "Program", "body":
[ { "type": "IfStatement", "test": { "type": "CallExpression", "callee": { "type": "Identifier", "name": "isEmpty" }, "arguments": [ { "type": "Literal", "value": "", "raw": "''" } ] }, "consequent": { "type": "BlockStatement", "body": [] }, "alternate": null } ], "sourceType": "script" } -
நߏจ +BWB4DSJQU if (isEmpty('')) { } { "type": "Program", "body":
[ { "type": "IfStatement", "test": { "type": "CallExpression", "callee": { "type": "Identifier", "name": "isEmpty" }, "arguments": [ { "type": "Literal", "value": "", "raw": "''" } ] }, "consequent": { "type": "BlockStatement", "body": [] }, "alternate": null } ], "sourceType": "script" } -
நߏจ +BWB4DSJQU { "type": "Program", "body": [ { "type": "IfStatement",
"test": { "type": "CallExpression", "callee": { "type": "Identifier", "name": "isEmpty" }, "arguments": [ { "type": "Literal", "value": "", "raw": "''" } ] }, "consequent": { "type": "BlockStatement", "body": [] }, "alternate": null } ], "sourceType": "script" } if (isEmpty('')) { } -
ߏจղੳͷදత࣮ख๏ w ࠶ؼԼ߱๏ w όοΫτϥοΫ͋Γ w ૉͳ࣮ w 1BSTFS$PNCJOBUPS w
1BSTFS&YQSFTTJPO(SBNNBS w όοΫτϥοΫͳ͠ w ʜ w ԋࢉࢠॱҐ๏ w ʜ ࠓճղઆ͢Δ࣮ -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ifจ> := "if " <ࣜ> " {\n" <จ>* "}" "\n" <ࣜจ> := <ࣜ> "\n" <ίϝϯτจ> := "//" [A-Z_ ]* "\n" <ࣜ> := <จࣈྻϦςϥϧ> | <ؔݺͼग़ࣜ͠> <จࣈྻϦςϥϧ> := "'" ("\'" | [A-Z_ ])* "'" <ؔݺͼग़ࣜ͠> := <໊ؔ> "(" <ࣜ> ")" <໊ؔ> := [a-z] [a-z]* ͜ͷ··ͩͱΘ͔ΓͮΒ͍ͷͰɺ۩ମྫΛग़͠·͢ -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ ͔͜͜ΒݟΔ // COMMENT ↩︎ EOF -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ จͷ܁Γฦ͠ʹଓ͍ͯ&0'͕͘Δ // COMMENT ↩︎ EOF -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ จͷதΛݟʹ͍͘ // COMMENT ↩︎ EOF -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ จ͜ͷ͏ͪͷͲΕ͔ͭ // COMMENT ↩︎ EOF -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ ࠓճͷจίϝϯτจ // COMMENT ↩︎ EOF -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτจͷதΛݟʹ͍͘ ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ // COMMENT ↩︎ EOF -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ʹଓ͍ͯӳେจࣈͱ@ͱۭന͕Կճ͔ଓ͍ͯɺվߦͰऴΘΔ ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ // COMMENT ↩︎ EOF -
// COMMENT ↩︎ EOF <ϓϩάϥϜ> := <จ>* EOF <จ> :=
<ifจ> | <ࣜจ> | <ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ -
// COMMENT ↩︎ EOF <ϓϩάϥϜ> := <จ>* EOF <จ> :=
<ifจ> | <ࣜจ> | <ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ ίʔυͱͷରԠ͕Ͱ͖ͨΒɺݟͨॱ൪ΛٯʹḪ͍ͬͯ͘ -
// COMMENT ↩︎ EOF <ϓϩάϥϜ> := <จ>* EOF <จ> :=
<ifจ> | <ࣜจ> | <ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" จίϝϯτจͰରԠ͕औΕͨͷͰḪΔ ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" // COMMENT EOF ࠓճͷจճ܁Γฦ͚ͩ͠Ͱɺதίϝϯτจ ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ίϝϯτจ> := "//" [A-Z_ ]* "\n" // COMMENT EOF ࠷ޙ&0'ͰऴΘΔ ίϝϯτ͚ͩϓϩάϥϜ ద༻͞Εͨจ๏نଇ -
print("TEXT") EOF ؔݺͼग़͚ͩ͠ϓϩάϥϜ ద༻͞Εͨจ๏نଇ <ϓϩάϥϜ> := <จ>* EOF <จ> :=
<ifจ> | <ࣜจ> | <ίϝϯτจ> <ࣜจ> := <ࣜ> "\n" <ࣜ> := <จࣈྻϦςϥϧ> | <ؔݺͼग़ࣜ͠> <ؔݺͼग़͠> := <໊ؔ> "(" <ࣜ> ")" <໊ؔ> := [a-z] [a-z]* -
print("TEXT") EOF ؔݺͼग़͚ͩ͠ϓϩάϥϜ ద༻͞Εͨจ๏نଇ <ϓϩάϥϜ> := <จ>* EOF <จ> :=
<ifจ> | <ࣜจ> | <ίϝϯτจ> <ࣜจ> := <ࣜ> "\n" <ࣜ> := <จࣈྻϦςϥϧ> | <ؔݺͼग़ࣜ͠> <ؔݺͼग़͠> := <໊ؔ> "(" <ࣜ> ")" <໊ؔ> := [a-z] [a-z]* ಉ͡Α͏ʹݟ͍͖ͯ·͠ΐ͏ -
print("TEXT") EOF ؔݺͼग़͚ͩ͠ϓϩάϥϜ ద༻͞Εͨจ๏نଇ <ϓϩάϥϜ> := <จ>* EOF <จ> :=
<ifจ> | <ࣜจ> | <ίϝϯτจ> <ࣜจ> := <ࣜ> "\n" <ࣜ> := <จࣈྻϦςϥϧ> | <ؔݺͼग़ࣜ͠> <ؔݺͼग़͠> := <໊ؔ> "(" <ࣜ> ")" <໊ؔ> := [a-z] [a-z]* <จࣈྻϦςϥϧ> -
<ϓϩάϥϜ> := <จ>* EOF <จ> := <ifจ> | <ࣜจ> |
<ίϝϯτจ> <ifจ> := "if " <ࣜ> " {\n" <จ>* "}" "\n" <ࣜจ> := <ࣜ> "\n" <ίϝϯτจ> := "//" [A-Z_ ]* "\n" <ࣜ> := <จࣈྻϦςϥϧ> | <ؔݺͼग़ࣜ͠> <จࣈྻϦςϥϧ> := "'" ("\'" | [A-Z_ ])* "'" <ؔݺͼग़ࣜ͠> := <໊ؔ> "(" <ࣜ> ")" <໊ؔ> := [a-z] [a-z]* ͜ͷ͝ͱʹநߏจͷϊʔυΛఆٛ͢Δ -
1SPHSBN 4UBUFNFOU 4UBUFNFOU # <ϓϩάϥϜ> := <จ>* class ProgramNode(Node): def
__init__(self, statement_nodes): # <ϓϩάϥϜ>ෳͷ<จ>͔ΒͳΔͷͰɺ # ؚ·ΕΔ<จ>ΛྻͰอ࣋͢Δɻ self.statements = statement_nodes ϓϩάϥϜʹରԠ͢Δநߏจͷϊʔυ จͷϊʔυͷྻ -
# <ϓϩάϥϜ> := <จ>* class ProgramNode(Node): def __init__(self, statement_nodes): #
<ϓϩάϥϜ>ෳͷ<จ>͔ΒͳΔͷͰɺ # ؚ·ΕΔ<จ>ΛྻͰอ࣋͢Δɻ self.statements = statement_nodes 4UBUFNFOU 4UBUFNFOU 1SPHSBN -
# <ϓϩάϥϜ> := <จ>* class ProgramNode(Node): def __init__(self, statement_nodes): #
<ϓϩάϥϜ>ෳͷ<จ>͔ΒͳΔͷͰɺ # ؚ·ΕΔ<จ>ΛྻͰอ࣋͢Δɻ self.statements = statement_nodes 1SPHSBN 4UBUFNFOU 4UBUFNFOU -
# <ifจ> := "if " <ࣜ> " {\n" จ* "}"
"\n" class IfStatementNode(Node): def __init__(self, condition_node, then_statements): # if ͷޙͷ݅෦ͷ <ࣜ> Λอ࣋͢Δɻ self.condition_node = condition_node # { ͱ } ͷؒͷෳͷ <จ> Λྻͱͯ͠อ࣋͢Δɻ self.then_statements = then_statements JGจʹରԠ͢Δநߏจͷϊʔυ -
# <ίϝϯτจ> := "//" [A-Z_ ]* "\n" def parse_comment_statement(text, index):
# TODO: ͪΌΜͱ࣮͢Δ next_index = 1234 # TODO: Ծͷ࣮ node = CommentStatementNode( comment_content=" COMMENT" # TODO: Ծͷ࣮ ) return node, next_index ίϝϯτจʹରԠ͢ΔؔͷҾͱΓΛ֬ೝ͠Α͏ ؔͷ෦·ͩԾͷͷͰɺ͋ͱͰ࣮͍ͯ͘͠ -
# <ίϝϯτจ> := "//" [A-Z_ ]* "\n" def parse_comment_statement(text, index):
# TODO: ͪΌΜͱ࣮͢Δ next_index = 1234 # TODO: Ծͷ࣮ node = CommentStatementNode( comment_content=" COMMENT" # TODO: Ծͷ࣮ ) return node, next_index ʮQBSTFʯʮߏจղੳ͢Δʯͷҙຯ -
# <ίϝϯτจ> := "//" [A-Z_ ]* "\n" def parse_comment_statement(text, index):
# TODO: ͪΌΜͱ࣮͢Δ next_index = 1234 # TODO: Ծͷ࣮ node = CommentStatementNode( comment_content=" COMMENT" # TODO: Ծͷ࣮ ) return node, next_index ղੳ͍ͨ͠ςΩετશମ ղੳ͍ͨ͠ςΩετͷҐஔ -
# <ίϝϯτจ> := "//" [A-Z_ ]* "\n" def parse_comment_statement(text, index):
# TODO: ͪΌΜͱ࣮͢Δ next_index = 1234 # TODO: Ծͷ࣮ node = CommentStatementNode( comment_content=" COMMENT" # TODO: Ծͷ࣮ ) return node, next_index ղੳͰ͖ͨίϝϯτจʹ ରԠ͢Δநߏจͷϊʔυ ίϝϯτจ͕ऴΘͬͨҐஔ -
Ҿ w ղੳ͢Δจࣈྻશମͱ࢝ΊΔҐஔ Γ w ղੳͰ͖ͨநߏจͷϊʔυͱऴΘͬͨҐஔ w ࣦഊͨ͠Β/POFΛฦͯ͠ɺ Ґஔಈ͔͞ͳ͍ ͜͜ͰҾͱΓΛཧ͠Α͏ɿ
͕ؔΈ߹Θͤ͘͢ͳΔͱ ݴ͍ͬͯͨͷ͜ͷ෦ લͷؔʹΑΔղੳ͕ऴΘͬͨ Ґஔ͔Βɺ࣍ͷؔʹΑΔղੳΛ ࠶։͢ΔΠϝʔδ -
<ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτจͷจ๏نଇͷಡΈํͷ͓͞Β͍ɿ ͡Ίʹ͕͋Δ
ଓ͚ͯӳେจࣈ͔@͔ۭന͕ճҎ্ଓ͘ ࠷ޙʹվߦ͕͋Δ -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index <ίϝϯτจ> := "//" [A-Z_ ]* "\n" ίϝϯτจʹରԠ͢Δؔ -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ͜͜Ͱ͕JOEFYͷҐஔʹ͋Δ͔Ͳ͏͔֬ೝ͍ͯ͠Δ FYQFDU@LFZXPSEޙͰઆ໌͢Δ͕ɺΛݟ͚ͭͨΒ JOEFYΛͷޙ·ͰҠಈͤ͞Δ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ͕Έ͔ͭΒͳ͚ΕίϝϯτจͰͳ͍ͷͰऴྃ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ͜ͷྻʹίϝϯτͷจࣈΛ֨ೲ͍ͯ͘͠ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ίϝϯτͷจࣈ܁Γฦ͠ͳͷͰɺϧʔϓͰॲཧ͢Δ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index JOEFYͷҐஔʹ<";@>͕͋Δ͔֬ೝ͍ͯ͠Δ FYQFDU@FJUIFSʹ͍ͭͯޙͰઆ໌͢Δ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ͠<";@>ʹҰக͠ͳ͔ͬͨΒɺίϝϯτͷ ༰෦͕ऴΘͬͨͷͰɺϧʔϓ͔Βग़ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ͠<";@>Ͱ͋Εɺίϝϯτจࣈྻ͕·ͩଓ͔͘ ͠Εͳ͍ͷͰɺจࣈΛهͯ͠ϧʔϓΛ࠶։ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ίϝϯτͷจࣈ͕ऴΘͬͨͷͰɺ࠷ޙʹ վߦ͕͋Δ͔Ͳ͏͔֬ೝ͍ͯ͠Δ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index aO͕ͳ͚ΕɺίϝϯτจͰͳ͍ͷͰ/POFͱ ݩͷJOEFYΛฦ͢ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ه͓͍ͯͨ͠ίϝϯτͷจࣈΛ݁߹ͯ͠ɺ ίϝϯτͷ༰จࣈྻΛ෮ݩ͢Δ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
def parse_comment_statement(text, index): is_success, next_index = expect_keyword("//", text, index) if
not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ෮ݩͨ͠ίϝϯτจࣈྻ͔ΒίϝϯτจʹରԠ͢Δ நߏจͷϊʔυΛ࡞ͯ͠ɺਐΊͨJOEFYͱฦ͢ <ίϝϯτจ> := "//" [A-Z_ ]* "\n" -
<ίϝϯτจ> := "//" [A-Z_ ]* "\n" def parse_comment_statement(text, index): is_success,
next_index = expect_keyword("//", text, index) if not is_success: return None, index comment_content_chars = [] while True: comment_content_char, next_index = \ expect_either(uppercase + [" ", "_"], text, next_index) if comment_content_char is None: break comment_content_chars.append(comment_content_char) is_success, next_index = expect_keyword("\n", text, next_index) if not is_success: return None, index comment_content_chars = "".join(comment_content_chars) return CommentStatementNode(comment_content_chars), next_index ίϝϯτจʹରԠ͢ΔؔΛॻ͚ͨ -
text = "abc" is_success, next_index = expect_keyword("a", text, 0) is_success,
next_index = expect_keyword("x", text, 0) Bͷ࣍ͷJOEFYͳͷͰ JOEFYͷҐஔʢͳͷͰઌ಄ʣʹB͕͋ΔͷͰ5SVF ͳ͍߹JOEFYΛม͑ͳ͍ͷͰ JOEFYͷҐஔʢͳͷͰઌ಄ʣʹYͳ͍ͷͰ'BMTF -
def expect_keyword(expected, text, index): expected_len = len(expected) actual = text[index:index+expected_len]
if actual != expected: return False, index return True, index + expected_len def expect_either(expected_list, text, index): for expected in expected_list: is_success, next_index = expect_keyword(expected, text, index) if is_success: return expected, next_index return None, index -
def expect_keyword(expected, text, index): expected_len = len(expected) actual = text[index:index+expected_len]
if actual != expected: return False, index return True, index + expected_len def expect_either(expected_list, text, index): for expected in expected_list: is_success, next_index = expect_keyword(expected, text, index) if is_success: return expected, next_index return None, index ৄࡉͳ࣮͋ͱͰղಡͯ͠Έ͍ͯͩ͘͞ʂ -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index ·ͣɺJGจʹରԠ͢ΔؔͰղੳͰ͖Δ͔Ͳ͏͔ࢼ͢ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index ͠ɺJGจͱͯ͠ͷղੳʹޭ͢ΕจJGจͩͬͨͱ͍͏͜ͱ JGจʹରԠ͢ΔநߏจͱɺJGจͷղੳͰਐΜͩJOEFYΛฦ͢ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index JGจͱͯ͠ͷղੳʹࣦഊͨ͠Βɺ࣍ʹࣜจͱͯ͠ͷղੳΛࢼ͢ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index ͠ɺࣜจͱͯ͠ͷղੳʹޭ͢Εจࣜจͩͬͨͱ͍͏͜ͱ ࣜจʹରԠ͢ΔநߏจͱɺࣜจͷղੳͰਐΜͩJOEFYΛฦ͢ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index ࣜจͱͯ͠ͷղੳʹࣦഊͨ͠Βɺ࠷ޙʹίϝϯτจͱͯ͠ͷղੳΛࢼ͢ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index ͠ɺίϝϯτจͱͯ͠ͷղੳʹޭ͢Εจίϝϯτจͩͬͨͱ͍͏͜ͱ ίϝϯτจʹରԠ͢ΔநߏจͱɺίϝϯτจͷղੳͰਐΜͩJOEFYΛฦ͢ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index JGจɺࣜจɺίϝϯτจͷ͍ͣΕͰͳ͚Εɺ จͰͳ͍ͷͰɺ/POFͱݩͷJOEFYΛฦͯ͠ऴྃ <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> -
def parse_statement(text, index): if_statement_node, next_index = parse_if_statement(text, index) if if_statement_node
is not None: return if_statement_node, next_index expr_statement_node, next_index = parse_expr_statement(text, index) if expr_statement_node is not None: return expr_statement_node, next_index comment_statement_node, next_index = parse_comment_statement(text, index) if comment_statement_node is not None: return comment_statement_node, next_index return None, index <จ> := <ifจ> | <ࣜจ> | <ίϝϯτจ> ͜ͷΑ͏ʹʹରԠ͢ΔؔΛఆ͍ٛͯ͘͠ͱɺ ࠷ऴతʹϓϩάϥϜʹରԠ͢ΔؔΛఆٛͰ͖Δ ͜ͷϓϩάϥϜʹରԠ͢Δ͕ؔɺҰൠతʹ ఏڙ͞Ε͍ͯΔߏจղੳثͱݺΕΔͷ -
ࠓճհͨ͠ૉͳ࣮Λ1BSTFS$PNCJOBUPSͰ࣮͢Δͱɺ ಉ͡ॲཧͳͷʹڻ͘΄Ͳ࣮͕͘ͳΓɺ͔ͭ࠶ར༻ੑ্͕͕Δɿ parse_comment = sequence( string("//"), sequence( many(char(uppercase + ["_",
" "])), string("\n") ) ) ؾʹͳΔਓɺεϥΠυඌͷࢀߟจݙ#Λࢀর -
def print_enter(node): print("enter: {node}".format(node=type(node))) def print_leave(node): print("leave: {node}".format(node=type(node))) traverse(node, print_enter,
print_leave) PO@FOUFSͱPO@MFBWF͕Θ͔ΔΑ͏ʹQSJOU͢ΔؔΛ༻ҙ -
1SPHSBN *G4UBUFNFOU 4USJOH-JUFSBM 'VOD$BMM $PNNFOU *EFOUJpFS QSJOU݁Ռ *EFOUJpFSʹࢠ͍ͳ͍ͷͰɺ*EFOUJpFS͔ΒΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS -
1SPHSBN *G4UBUFNFOU 'VOD$BMM $PNNFOU *EFOUJpFS 4USJOH-JUFSBM QSJOU݁Ռ 'VOD$BMMͷ࣍ͷࢠͰ͋Δ4USJOH-JUFSBMΛ๚ΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM -
1SPHSBN *G4UBUFNFOU 'VOD$BMM $PNNFOU *EFOUJpFS 4USJOH-JUFSBM QSJOU݁Ռ 4USJOH-JUFSBMʹࢠ͍ͳ͍ͷͰɺ4USJOH-JUFSBM͔ΒΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM -
1SPHSBN *G4UBUFNFOU $PNNFOU *EFOUJpFS 4USJOH-JUFSBM 'VOD$BMM QSJOU݁Ռ 'VOD$BMMͷͯ͢ͷࢠΛ๚ͨ͠ͷͰɺ'VOD$BMM͔ΒΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM MFBWF'VOD$BMM -
1SPHSBN *G4UBUFNFOU *EFOUJpFS 4USJOH-JUFSBM 'VOD$BMM $PNNFOU QSJOU݁Ռ *G4UBUFNFOUͷ࣍ͷࢠͰ͋Δ$PNNFOUΛ๚ΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM MFBWF'VOD$BMM FOUFS$PNNFOU -
1SPHSBN *G4UBUFNFOU *EFOUJpFS 4USJOH-JUFSBM 'VOD$BMM $PNNFOU QSJOU݁Ռ $PNNFOUʹࢠ͍ͳ͍ͷͰɺ$PNNFOU͔ΒΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM MFBWF'VOD$BMM FOUFS$PNNFOU MFBWF$PNNFOU -
1SPHSBN *EFOUJpFS 4USJOH-JUFSBM 'VOD$BMM $PNNFOU *G4UBUFNFOU QSJOU݁Ռ *G4UBUFNFOUͷͯ͢ͷࢠΛ๚ͨ͠ͷͰɺΕΔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM MFBWF'VOD$BMM FOUFS$PNNFOU MFBWF$PNNFOU MFBWF*G4UBUFNFOU -
*EFOUJpFS 4USJOH-JUFSBM 'VOD$BMM $PNNFOU *G4UBUFNFOU 1SPHSBN QSJOU݁Ռ 1SPHSBNͷͯ͢ͷࢠΛ๚ͨ͠ͷͰɺࠪྃ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM MFBWF'VOD$BMM FOUFS$PNNFOU MFBWF$PNNFOU MFBWF*G4UBUFNFOU MFBWF1SPHSBN -
*EFOUJpFS 4USJOH-JUFSBM 'VOD$BMM $PNNFOU *G4UBUFNFOU 1SPHSBN QSJOU݁Ռ ཁ͢ΔʹɺநߏจΛਂ͞༏ઌ୳ࡧ͢Δؔ FOUFS1SPHSBN FOUFS*G4UBUFNFOU
FOUFS'VOD$BMM FOUFS*EFOUJpFS MFBWF*EFOUJpFS FOUFS4USJOH-JUFSBM MFBWF4USJOH-JUFSBM MFBWF'VOD$BMM FOUFS$PNNFOU MFBWF$PNNFOU MFBWF*G4UBUFNFOU MFBWF1SPHSBN -
class Node: def children(self): raise NotImplementedError() class ProgramNode(Node): def __init__(self,
statement_nodes): self.statements = statement_nodes def children(self): return self.statements நߏจͷϊʔυ͕ࣗͷࢠΛฦ͢Α͏ʹཁٻ͢ΔجఈΫϥεΛ༻ҙ -
class Node: def children(self): raise NotImplementedError() class ProgramNode(Node): def __init__(self,
statement_nodes): self.statements = statement_nodes def children(self): return self.statements ͦΕͧΕͷநߏจͷϊʔυʹ࣮ͯ͠Β͏ -
class Node: def children(self): raise NotImplementedError() class ProgramNode(Node): def __init__(self,
statement_nodes): self.statements = statement_nodes def children(self): return self.statements ϓϩάϥϜͷͯ͢ͷࢠͷҰཡΛྻͱͯ͠ฦ͢ ࠓճจͷྻҎ֎ʹࢠͳ͍ͷͰͦͷ··ฦͤΑ͍ -
class IfStatementNode(Node): def __init__(self, condition_node, then_statements): self.condition_node = condition_node self.then_statements
= then_statements def children(self): children = [self.condition_node] return children + self.then_statements ผͷέʔεͱͯ͠ɺJGจͷநߏจͷϊʔυΈͯΈΑ͏ -
class IfStatementNode(Node): def __init__(self, condition_node, then_statements): self.condition_node = condition_node self.then_statements
= then_statements def children(self): children = [self.condition_node] return children + self.then_statements JGจͷࢠ݅෦ͷࣜͱɺ\^ͷதͷ ෳͷจͳͷͰɺ·ͱΊͯҰཡʹͯ͠ฦ͢ -
class IfStatementNode(Node): def __init__(self, condition_node, then_statements): self.condition_node = condition_node self.then_statements
= then_statements def children(self): children = [self.condition_node] return children + self.then_statements ಉ༷ʹɺͯ͢ͷநߏจͷϊʔυʹDIJMESFOΛ࣮ͤ͞Δ -
def traverse(node, on_enter=None, on_leave=None): if on_enter is not None: on_enter(node)
for child in node.children(): traverse(child, on_enter, on_leave) if on_leave is not None: on_leave(node) -
def traverse(node, on_enter=None, on_leave=None): if on_enter is not None: on_enter(node)
for child in node.children(): traverse(child, on_enter, on_leave) if on_leave is not None: on_leave(node) நߏจͷϊʔυΛ๚ΕͨͷͰPO@FOUFSΛ࣮ߦ͢Δ ͠PO@FOUFS͕লུ͞Ε͍ͯΔ߹Կ͠ͳ͍ -
def traverse(node, on_enter=None, on_leave=None): if on_enter is not None: on_enter(node)
for child in node.children(): traverse(child, on_enter, on_leave) if on_leave is not None: on_leave(node) நߏจͷϊʔυ͔ΒࢠͷҰཡΛ औಘ͠ɺࢠΛ࠶ؼతʹ͍ࠪͯ͘͠ -
def traverse(node, on_enter=None, on_leave=None): if on_enter is not None: on_enter(node)
for child in node.children(): traverse(child, on_enter, on_leave) if on_leave is not None: on_leave(node) ࢠͷ͕ࠪऴΘͬͨͷͰɺࠓͷϊʔυ͔Βୀग़͢Δ -
def find_nodes(node, condition): found_nodes = [] def collect_nodes(entered_node): if not
condition(entered_node): return found_nodes.append(entered_node) traverse(node, collect_nodes) return found_nodes -
def find_nodes(node, condition): found_nodes = [] def collect_nodes(entered_node): if not
condition(entered_node): return found_nodes.append(entered_node) traverse(node, collect_nodes) return found_nodes ݕࡧ݅ʹ͋ͯ·ΔநߏจͷϊʔυΛूΊΔྻ -
def find_nodes(node, condition): found_nodes = [] def collect_nodes(entered_node): if not
condition(entered_node): return found_nodes.append(entered_node) traverse(node, collect_nodes) return found_nodes நߏจͷϊʔυΛ๚ΕΔλΠϛϯάͰݕࡧ݅ͷ ؔΛ࣮͠ɺ݅ʹ͋ͯ·ΔϊʔυΛूΊΔؔ -
def find_nodes(node, condition): found_nodes = [] def collect_nodes(entered_node): if not
condition(entered_node): return found_nodes.append(entered_node) traverse(node, collect_nodes) return found_nodes நߏจͷϊʔυΛूΊΔؔΛ PO@FOUFSʹࢦఆͯࠪ͠Λ։࢝ -
def find_nodes(node, condition): found_nodes = [] def collect_nodes(entered_node): if not
condition(entered_node): return found_nodes.append(entered_node) traverse(node, collect_nodes) return found_nodes ݟ͔ͭͬͨநߏจͷϊʔυͷྻΛฦ͢ -
def find_nodes(node, condition): found_nodes = [] def collect_nodes(entered_node): if not
condition(entered_node): return found_nodes.append(entered_node) traverse(node, collect_nodes) return found_nodes ࠪؔΛ͏͜ͱͰɺͱͯ؆୯ʹ࣮Ͱ͖ͨ -
ࠪؔͷ࣮Ͱઆ໌͠ͳ͔ͬͨ͜ͱ w ࠪͷதஅࢠͷ୳ࡧͷεΩοϓػೳ w ύϑΥʔϚϯεվળͷͨΊʹॏཁ w ୈڃؔΛΘͳ͍ࠪؔͷ࣮ w ݴޠʹΑͬͯؔΛҾʹͤͳ͍͜ͱ͋Δ w
ͦͷ߹(P'ͷ7JTJUPSύλʔϯͰସՄೳ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ίʔυ͔ΒੜҙؾͳίϝϯτΛݟ͚ͭͯΛใࠂ͢Δؔ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ίʔυΛߏจղੳͯ͠நߏจΛ࡞͢Δ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ߏจղੳʹࣦഊͨ͠ΒɺߏจΤϥʔΛදࣔ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ࠪؔͷར༻ྫͱ࣮ͯͨ͠͠ɺݕࡧؔΛͬͯੜҙؾͳίϝϯτΛूΊΔ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ݕࡧؔͷ݅ʹࢦఆͨ͠ɺੜҙؾίϝϯτ͔Ͳ͏͔ఆ͢Δؔ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ·ͣɺநߏจ͕ίϝϯτจͰͳ͚Εɺ ੜҙؾͳίϝϯτͰͳ͍ͷͰɺ'BMTFΛฦͯ͠ऴྃ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ίϝϯτจͩͬͨ߹ɺίϝϯτͷ༰ʹੜҙؾͳ ͷؚ͕·Ε͍ͯΕ5SVFɺͦΕҎ֎'BMTFΛฦ͢ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ઌ΄ͲͷؔͰ5SVF͕ฦ͞Εͨநߏจʢੜҙؾͳίϝϯτʣ͕ Ұཡʹͯ͠ฦ͞ΕΔ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ੜҙؾͳίϝϯτͷΛใࠂͯ͠ऴྃ -
def report_namaiki_comments(text): node = parse_program(text) if node is None: print("Syntax
Error!") return namaiki_comments = find_nodes(node, is_namaiki_comment) print("Found {count} comments!".format(count=len(namaiki_comments))) def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ߏจղੳثɾࠪؔɾݕࡧؔΛ Έ߹Θͤͯ͏͜ͱͰɺͱͯ ؆୯ʹ੩తݕࠪΛ࣮Ͱ͖ͨ -
class Policy: def get_violations(self, node): raise NotImplementedError() ྫ͑ɺҾʹੜҙؾͳίϝϯτจ͕͖ͨΒ ࢦఠϝοηʔδΛฦ࣮͢ɺJGจ͕͖ͨΒ ݅෦ͷࣜΛݕࠪ͢Δͧɺͱ͍͏࣮͕͋Γ͑Δ
ॏཁͳͷɺݕࠪϧʔϧ͕ผͳΒผͷΫϥεʹ ׂ࣮ͯ͢͠Δ͜ͱɻҰॹʹ͢Δͱอक͕େมʹͳΔ -
class NamaikiCommentPolicy(Policy): def get_violations(self, node): if not is_namaiki_comment(node): return []
return ["FOUND YOU!"] def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content લʹ࣮ͨ͠ͷͱಉ͡ɺੜҙؾͳίϝϯτจΛࢦఠ͢Δݕࠪϧʔϧ -
class NamaikiCommentPolicy(Policy): def get_violations(self, node): if not is_namaiki_comment(node): return []
return ["FOUND YOU!"] def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content Ҿͷநߏจͷϊʔυ͕ੜҙؾͳίϝϯτจʼͳΒɺ ʮݟ͚ͭͨͧʂʯͱ͍͏ϝοηʔδΛฦ͢Α͏ʹ࣮ -
class NamaikiCommentPolicy(Policy): def get_violations(self, node): if not is_namaiki_comment(node): return []
return ["FOUND YOU!"] def is_namaiki_comment(entered_node): if not isinstance(entered_node, CommentStatementNode): return False comment_node = entered_node return "CATCH_ME_IF_YOU_CAN" in comment_node.comment_content ੜҙؾͳίϝϯτจΛఆ͢ΔؔલͷͷΛྲྀ༻ -
class Policies(Policy): def __init__(self, policies): self.policies = policies def get_violations(self,
node): return reduce( lambda violations, policy: violations + policy.get_violations(node), self.policies, [] ) ෳͷݕࠪϧʔϧΛଋͶΔݕࠪϧʔϧΛ४උ -
class Policies(Policy): def __init__(self, policies): self.policies = policies def get_violations(self,
node): return reduce( lambda violations, policy: violations + policy.get_violations(node), self.policies, [] ) ݕࠪϧʔϧͱಉ͡ڞ௨ΠϯλʔϑΣʔεΛ࣮͢Δ -
class Policies(Policy): def __init__(self, policies): self.policies = policies def get_violations(self,
node): return reduce( lambda violations, policy: violations + policy.get_violations(node), self.policies, [] ) ෦ͷෳͷݕࠪϧʔϧΛϧʔϓͯ͠ݕࠪͯ͠ɺ݁ՌΛ߹ͯ͠ฦ͢ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ෳͷݕࠪϧʔϧͰ·ͱΊͯݕࠪ͢Δؔ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ༩͑ΒΕͯจࣈྻΛߏจղੳͯ͠நߏจΛ࡞ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ߏจղੳʹࣦഊͨ͠ΒɺߏจղੳࣦഊͷϝοηʔδΛ݁Ռͱͯ͠ฦ͢ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ෳͷݕࠪϧʔϧΛଋͶͯɺͭͷݕࠪϧʔϧʹ·ͱΊΔ ʢ͜ͷྫͰɺੜҙؾͳίϝϯτΛݕࠪ͢Δϧʔϧ͚ͩʣ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ݕࠪϝοηʔδΛूΊΔྻΛ४උ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ༩͑ΒΕͨநߏจͷϊʔυ͔ΒɺݕࠪϝοηʔδΛूΊΔؔ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) நߏจΛࠪͯ͠ɺݕࠪϝοηʔδΛूΊ͍ͯ͘ -
def lint(text): node = parse_program(text) if node is None: return
["Syntax Error!"] policies = Policies([NamaikiCommentPolicy()]) violations_buffer = [] def collect_violations(entered_node): violations = policies.get_violations(entered_node) violations_buffer.append(violations) traverse(node, collect_violations) all_violations = [] return reduce(add, violations_buffer, all_violations) ू·ͬͨݕࠪϝοηʔδΛͭͷྻʹ·ͱΊͯฦ͢ -
1ZUIPO w ߏจղੳث w BTUQBSTFʢඪ४ϥΠϒϥϦʣ w ࠪؔ w BTU/PEF7JTJUPSʢඪ४ϥΠϒϥϦʣ w
੩తݕࠪϧʔϧ w $IFDLFS53:ʢQZqBLFTͷQZqBLTDIFDLFSQZʣ -
3VCZ w ߏจղੳث w 1BSTFS3VCZ (JU)VCͷXIJUFRVBSLQBSTFSͷMJCQBSTFSSVCZZ w ࠪؔ w
3VCPDPQ"455SBWFSTBM 3VCPDPQͷMJCSVCPDPQBTUUSBWFSTBMSC w ੩తݕࠪϧʔϧ w 3VCPDPQ$PQ-JOU&NQUZ&OTVSF 3VCPDPQͷMJCSVCPDPQDPQMJOUFNQUZ@FOTVSFSC -
+BWB4DSJQU w ߏจղੳث w 1BSTFSʢ(JU)VCͷBDPSOKTBDPSOͷTSDTUBUFKT w ࠪؔ w 5SBWFSTFUSBWFSTF
&4-JOUͷMJCVUJMUSBWFSTFKT w ੩తݕࠪϧʔϧ w SVMFTOPFNQUZGVODUJPO &4-JOUͷMJCSVMFTOPFNQUZGVODUJPOKT -
$44ͷநߏจΛൺֱ͢Δπʔϧ https://github.com/mixi-inc/css-semdiff $ css-astdiff a.css b.css --verbose extra: .extra-1 {
border: none; } missing: .missing-1 { border: none; } --------------------------------- 1 extra rules and 1 missing rules -
·ͱΊ ੩తղੳΛߏ͢Δجຊతͳߟ͑ํΛհɿ w நߏจ w ߏจղੳ w ࠪؔ w ݕࠪϧʔϧͷׂ
੩తղੳҙ֎ͱ؆୯ʹ࣮Ͱ͖·͢