SlideShare ist ein Scribd-Unternehmen logo
1 von 33
HTML Output
<article>
<h1>Title</h1>
<p>Body</p>
</article>
Pug Source
article
h1 Title
p Body
Pug Compiler
Lexer Parser Code Gen
Lexer
article
h1 Title
p Body
• tag – "article"
• indent
• tag – "h1"
• text – "Title"
• new line
• tag – "p"
• text – "Body"
• outdent
Lexer
class Lexer {
constructor(str) {
this.str = str;
this.indent = 0;
}
}
Lexer
tag() {
let match = /^[a-z]+/.exec(this.str);
if (match) {
this.str = this.str.substr(match[0].length);
return {type: 'tag', val: match[0]};
}
}
Lexer
text() {
let match = /^ .+/.exec(this.str);
if (match) {
this.str = this.str.substr(match[0].length);
return {type: 'text', val: match[0].substr(1)};
}
}
Lexer
newline() {
let match = /^n( *)/.exec(this.str);
if (match) {
this.str = this.str.substr(match[0].length);
let oldIndent = this.indent;
this.indent = match[1].length;
if (this.indent > oldIndent) return {type: 'indent'};
if (this.indent < oldIndent) return {type: 'outdent'};
return {type: 'newline'};
}
}
Lexer
fail() {
throw new Error(
'Unexpected text'
);
}
getTokens() {
let tokens = [];
while (this.str.length) {
tokens.push(
this.newline() ||
this.tag() ||
this.text() ||
this.fail()
);
}
tokens.push({type: 'eos'});
return tokens;
}
Parser
OutdentIndent
New
Line
Text
"Body"
Tag
"h1"
Tag
"article"
Text
"Title"
Tag
"p"
Parser
Text
"Body"
Tag
"h1"
Tag
"article"
Text
"Title"
Tag
"p"
Parser
class Parser {
constructor(tokens) {
this.tokens = new TokenStream(tokens);
}
}
Token Stream
• Next – get the next token and advance the stream
• Peek – get the next token without advancing the stream
• Expect(type) – get the next token and advance the stream
Parser
parseFile() {
let nodes = [];
while (this.tokens.peek().type !== 'eos') {
if (this.tokens.peek().type === 'tag') {
nodes.push(this.parseTag());
} else {
this.tokens.expect('newline');
}
}
this.tokens.expect('eos');
return {type: 'File', nodes};
}
Parser
parseTag() {
let tok = this.tokens.expect('tag');
let body = null;
switch (this.tokens.peek().type) {
case 'text':
body = this.parseText();
break;
case 'indent':
body = this.parseBlock();
break;
}
return {type: 'Tag', name: tok.val, body};
}
Parser
parseBlock() {
this.tokens.expect('indent');
let nodes = [];
while (this.tokens.peek().type !== 'outdent') {
if (this.tokens.peek().type === 'tag') {
nodes.push(this.parseTag());
} else {
this.tokens.expect('newline');
}
}
this.tokens.expect('outdent');
return {type: 'Block', nodes};
}
Code Gen
Text
"Body"
Tag
"h1"
Tag
"article"
Text
"Title"
Tag
"p"
Code Gen
Text
"Body"
Tag
"h1"
Tag
"article"
"Title"
Tag
"p"
Code Gen
Text
"Body"
"<h1>Title</h1>"
Tag
"article"
Tag
"p"
Code Gen
"Body"
"<h1>Title</h1>"
Tag
"article"
Tag
"p"
Code Gen
"<h1>Title</h1>"
Tag
"article"
"<p>Body</p>"
Code Gen "<article><h1>Title</h1><p>Body</p></article>"
Code Gen
function render(node) {
switch (node.type) {
case 'Block':
return renderBlock(node);
case 'Tag':
return renderTag(node);
case 'Text':
return renderText(node);
}
}
Code Gen
function renderBlock(node) {
return node.nodes.map(render).join('n');
}
Code Gen
function renderTag(node) {
if (!node.body) return '<' + node.name + '/>';
return (
'<' + node.name + '>' +
render(node.body) +
'</' + node.name + '>'
);
}
Code Gen
function renderText(node) {
return node.val;
}
Pug Compiler
Lexer Parser Code Gen
Includes
article
h1 Title
include ./content.pug
Linking
File A
Include
File B
FILE B
Linking
File A
Include
File B
FILE B
Pug Compiler Pipeline
Lexer Parser Loader Linker Code-Gen
String Tokens AST Collection
of ASTs
AST String
Lexer Parser Loader Linker Code-Gen
Now it's your turn!
String Tokens AST Collection
of ASTs
AST String

Weitere ähnliche Inhalte

Was ist angesagt?

The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189Mahmoud Samir Fayed
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession
 
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09Ilya Grigorik
 
using python module: doctest
using python module: doctestusing python module: doctest
using python module: doctestmitnk
 
Better Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQLBetter Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQLArtur Zakirov
 
Implementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional ProgramingImplementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional ProgramingVincent Pradeilles
 
Drinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of ResilienceDrinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of ResilienceC4Media
 
Git Tutorial Yang Yang
Git Tutorial Yang YangGit Tutorial Yang Yang
Git Tutorial Yang YangYang Yang
 
Productive Programming in Groovy
Productive Programming in GroovyProductive Programming in Groovy
Productive Programming in GroovyGanesh Samarthyam
 
Logic Equations Resolver J Script
Logic Equations Resolver   J ScriptLogic Equations Resolver   J Script
Logic Equations Resolver J ScriptRoman Agaev
 
Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017André Goliath
 

Was ist angesagt? (17)

The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189
 
Crawler 2
Crawler 2Crawler 2
Crawler 2
 
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofitjSession #4 - Maciej Puchalski - Zaawansowany retrofit
jSession #4 - Maciej Puchalski - Zaawansowany retrofit
 
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
 
Linux intro 4 awk + makefile
Linux intro 4  awk + makefileLinux intro 4  awk + makefile
Linux intro 4 awk + makefile
 
Full Text Search in PostgreSQL
Full Text Search in PostgreSQLFull Text Search in PostgreSQL
Full Text Search in PostgreSQL
 
Meet.js promises
Meet.js promisesMeet.js promises
Meet.js promises
 
using python module: doctest
using python module: doctestusing python module: doctest
using python module: doctest
 
2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
 
Better Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQLBetter Full Text Search in PostgreSQL
Better Full Text Search in PostgreSQL
 
Implementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional ProgramingImplementing pseudo-keywords through Functional Programing
Implementing pseudo-keywords through Functional Programing
 
Drinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of ResilienceDrinking from the Elixir Fountain of Resilience
Drinking from the Elixir Fountain of Resilience
 
Git Tutorial Yang Yang
Git Tutorial Yang YangGit Tutorial Yang Yang
Git Tutorial Yang Yang
 
Productive Programming in Groovy
Productive Programming in GroovyProductive Programming in Groovy
Productive Programming in Groovy
 
Pdxpugday2010 pg90
Pdxpugday2010 pg90Pdxpugday2010 pg90
Pdxpugday2010 pg90
 
Logic Equations Resolver J Script
Logic Equations Resolver   J ScriptLogic Equations Resolver   J Script
Logic Equations Resolver J Script
 
Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017Rest in Good! - DevCon 2017
Rest in Good! - DevCon 2017
 

Ähnlich wie Pug - a compiler pipeline

Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...adrianoalmeida7
 
Spark with Elasticsearch
Spark with ElasticsearchSpark with Elasticsearch
Spark with ElasticsearchHolden Karau
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String FunctionsAvanitrambadiya
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Workhorse Computing
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua LawrenceEmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua LawrenceJoshua Lawrence
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overviewAveic
 
An Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesAn Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesRay Toal
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio
 
Ast transformations
Ast transformationsAst transformations
Ast transformationsHamletDRC
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdfanandatalapatra
 

Ähnlich wie Pug - a compiler pipeline (20)

webScrapingFunctions
webScrapingFunctionswebScrapingFunctions
webScrapingFunctions
 
Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
 
Javascript2839
Javascript2839Javascript2839
Javascript2839
 
Json
JsonJson
Json
 
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
Cypher inside out: Como a linguagem de pesquisas em grafo do Neo4j foi constr...
 
Unfiltered Unveiled
Unfiltered UnveiledUnfiltered Unveiled
Unfiltered Unveiled
 
WOTC_Import
WOTC_ImportWOTC_Import
WOTC_Import
 
Spark with Elasticsearch
Spark with ElasticsearchSpark with Elasticsearch
Spark with Elasticsearch
 
Javascript built in String Functions
Javascript built in String FunctionsJavascript built in String Functions
Javascript built in String Functions
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Linq introduction
Linq introductionLinq introduction
Linq introduction
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua LawrenceEmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
EmberConf 2021 - Crossfile Codemodding with Joshua Lawrence
 
Postgresql 9.3 overview
Postgresql 9.3 overviewPostgresql 9.3 overview
Postgresql 9.3 overview
 
Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
An Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax TreesAn Annotation Framework for Statically-Typed Syntax Trees
An Annotation Framework for Statically-Typed Syntax Trees
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
 
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
FNT 2015 PDIS CodeEU - Zanimljiva informatika - 02 Djordje Pavlovic - Live_ch...
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 

Kürzlich hochgeladen

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 

Kürzlich hochgeladen (20)

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 

Pug - a compiler pipeline

Hinweis der Redaktion

  1. How many of you use one of these? Raise your hand if you use Webpack or Browserify [Walk] Today I hope to give you a better idea of how these tools work, and give you the tools to build and contribute to them yourself.
  2. Many of us write html every day It's verbose It lacks features for reusability and dynamic content
  3. I maintain a language that used to be called "jade". Today I'm announcing that, along with the release of version 2.0.0, it has been renamed to "pug" Pug is a simple language for producing HTML documents. It has a concise syntax, and supports features for code reuse and dynamic content.
  4. There are three main stages to the pug compiler: [walk] Lex the source code to convert the string of text into a stream of tokens Parse the tokens into a tree structure called an abstract syntax tree Generate output code from the abstract syntax tree
  5. The lexer splits the stream of characters from the source code into logical tokens The first logical token here is the "article" tag, since we read that as a single unit of meaning
  6. The lexer can be thought of as a simple state machine. For our language, it needs to keep track of: The remaining source code that has not yet been lexed. The current level of indentation
  7. We'll define a function for each type of token. Each function will: Match the string against a regular expression If it matches, it will consume those characters Then return a token of the appropriate tag If it does not match, we return `undefined` and don't consume any characters For the tag, we simply match any number of characters in the range a to z
  8. The function for text is mostly the same. We match a space, followed by any characters to the end of the line. We then remove the leading space from the value we store in the token.
  9. Our handling of newlines is a little different. We'll use one function to track indents, outdents and new lines. We match a newline followed by any number of spaces. We then set the number of spaces as the new value of this.indent and compare the new indent against the old indent to decide what token type to return.
  10. To generate the complete stream of tokens we just repeatedly call these methods until there is no more text. Note how if each token type doesn't match, we simply fall through to the next token type. The last token type we consider is `fail`, which just throws an error to indicate that there was some unexpected text. Finally, we push an "end of stream" token, to indicate that there are no more characters to read.
  11. Once the lexer has generated a stream of tokens, like this one, it is the job of the parser to transform that stream of tokens into the logical tree structure that matches how we (as programmers) view the code. [walk] Notice how the "article" contains the "h1" and the "p"
  12. Once the lexer has generated a stream of tokens, like this one, it is the job of the parser to transform that stream of tokens into the logical tree structure that matches how we (as programmers) view the code. [walk] Notice how the "article" contains the "h1" and the "p"
  13. Our parser starts with a stream of tokens
  14. At the top level, we keep looking to parse a new tag until we get to the end of the stream. Start by defining the list of nodes, then while the next token is not "end of stream": - if it's a tag, parseTag - if it's a newline, ignore it
  15. When parsing a tag, we call into `parseText` or `parseBlock` to recursively pass the content of the tag, depending on the type of the next token.
  16. Parsing a block looks just like parsing the file, except that instead of an "end of stream" token, we are looking for an "outdent" token.
  17. Now we have this "abstract syntax tree", we need to generate some output code from it. We'll recursively convert each node of the tree from the bottom up [walk]
  18. The code for this starts with a render method. It will recursively call the appropriate function to render each type of node. Note how these methods mirror the methods of the parser.
  19. To render a block, we simply render all the nodes within it.
  20. To render a tag, we simply build the bits of text that the tag itself provides, and then recursively render the content.
  21. Rendering text is just a case of returning the text
  22. [walk] [emphasis] [smile] Now you have hopefully seen how the parts of the compiler fit together
  23. Pug also supports splitting your code into multiple files. One of the simplest ways it does this is via `includes.
  24. An include results in two separate abstract syntax trees, which must be joined together. To do this without complicating the existing stages of our compiler, we will add an additional stage to our compiler.
  25. An include results in two separate abstract syntax trees, which must be joined together. To do this without complicating the existing stages of our compiler, we will add an additional stage to our compiler.
  26. Lets review the stages of this new compiler pipeline Talk through stages [walk] Clear Separation of Stages Clear Extension Points Uses our own extension points where possible
  27. SMILE!