![Page 1: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/1.jpg)
Parsing CSS with Boost.Spirit.X3
Ruben Van Boxem
February 4th 2019
![Page 2: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/2.jpg)
Introduction
![Page 3: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/3.jpg)
Introduction whoami?
About me
https://github.com/rubenvb
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 1 / 45
![Page 4: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/4.jpg)
Introduction Context songtext
SkUI
Modern C++next UI framework:
• signal/slot
• friendly API
• fast
• platform integration
All help is welcome: www.github.com/skui-org/skui
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 2 / 45
![Page 5: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/5.jpg)
Introduction Context songtext
SkUI
Modern C++next UI framework:
• signal/slot
• friendly API
• fast
• platform integration
All help is welcome: www.github.com/skui-org/skui
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 2 / 45
![Page 6: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/6.jpg)
Introduction Context songtext
SkUI
Modern C++next UI framework:
• signal/slot
• friendly API
• fast
• platform integration
All help is welcome: www.github.com/skui-org/skui
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 2 / 45
![Page 7: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/7.jpg)
Introduction Context songtext
SkUI
Modern C++next UI framework:
• signal/slot
• friendly API
• fast
• platform integration
All help is welcome: www.github.com/skui-org/skui
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 2 / 45
![Page 8: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/8.jpg)
Introduction Context songtext
SkUI
Modern C++next UI framework:
• signal/slot
• friendly API
• fast
• platform integration
All help is welcome: www.github.com/skui-org/skui
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 2 / 45
![Page 9: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/9.jpg)
Introduction Introductory introduction
About this presentation
Introduction
Representing input
Practical Boost.Spirit
CSS
CSS in Spirit
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 3 / 45
![Page 10: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/10.jpg)
Representing input
![Page 11: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/11.jpg)
Representing input Grammatical syntax
EBNF: Extended Backus Naur Form
A representation of a context-free grammar that can express itself:
l e t t e r = "A" | "B " | "C" | "D" | " E " | " F " | "G" | "H" | " I " | " J " | "K" | " L " | "M" | "N"| "O" | " P " | "Q" | " R " | " S " | " T " | "U" | "V" | "W" | " X " | " Y " | "Z " | " a " | " b "| " c " | " d " | " e " | " f " | " g " | " h " | " i " | " j " | " k " | " l " | "m" | " n " | " o " | " p "| " q " | " r " | " s " | " t " | " u " | " v " | "w" | " x " | " y " | " z " ;
d i g i t = " 0 " | " 1 " | " 2 " | " 3 " | " 4 " | " 5 " | " 6 " | " 7 " | " 8 " | " 9 " ;symbol = " [ " | " ] " | " { " | " } " | " ( " | " ) " | " < " | " > "
| " ' " | ' " ' | " = " | " | " | " . " | " , " | " ; " ;c h a r a c t e r = l e t t e r | d i g i t | symbol | " _ " ;
i d e n t i f i e r = l e t t e r , { l e t t e r | d i g i t | " _ " } ;t e r m i n a l = " ' " , c h a r a c t e r , { c h a r a c t e r } , " ' "
| ' " ' , c h a r a c t e r , { c h a r a c t e r } , ' " ' ;
l h s = i d e n t i f i e r ;rh s = i d e n t i f i e r
| t e r m i n a l| " [ " , rh s , " ] "| " { " , rh s , " } "| " ( " , rh s , " ) "| rh s , " | " , rh s| rhs , " , " , rh s ;
r u l e = l h s , " = " , rhs , " ; " ;grammar = { r u l e } ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 4 / 45
![Page 12: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/12.jpg)
Representing input Grammatical syntax
Reading input
Generally two ways to process input:
1. hand-wri�en: recursive decent, state machine, other “ingenious" designs . . .
2. generated: ANTLR, YACC/Bison, Elkhound, Whale Calf, and some other largewild animals or parts thereof.
Trade-o� between
• learning curve
• readability & expressiveness
• maintainability
• bug-proneness
• performance
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 5 / 45
![Page 13: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/13.jpg)
Representing input Grammatical syntax
Reading input
Generally two ways to process input:
1. hand-wri�en: recursive decent, state machine, other “ingenious" designs . . .
2. generated: ANTLR, YACC/Bison, Elkhound, Whale Calf, and some other largewild animals or parts thereof.
Trade-o� between
• learning curve
• readability & expressiveness
• maintainability
• bug-proneness
• performance
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 5 / 45
![Page 14: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/14.jpg)
Representing input Of bytes, tokens, and the meaning of life
Steps and stones
input - “stream of bytes"
↓
lex: spli�ing into tokens
↓
parse: transform into more useful representation
↓
AST: Abstract Syntax Tree: data structure corresponding to original input
↓
Make millions
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 6 / 45
![Page 15: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/15.jpg)
Representing input Of bytes, tokens, and the meaning of life
Steps and stones
input - “stream of bytes"
↓
lex: spli�ing into tokens
↓
parse: transform into more useful representation
↓
AST: Abstract Syntax Tree: data structure corresponding to original input
↓
Make millions
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 6 / 45
![Page 16: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/16.jpg)
Representing input Of bytes, tokens, and the meaning of life
Steps and stones
input - “stream of bytes"
↓
lex: spli�ing into tokens
↓
parse: transform into more useful representation
↓
AST: Abstract Syntax Tree: data structure corresponding to original input
↓
Make millions
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 6 / 45
![Page 17: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/17.jpg)
Practical Boost.Spirit
![Page 18: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/18.jpg)
Practical Boost.Spirit Borderline abuse, C++ style
C++ talks grammar
Can we make C++ work for us?
1. express the content, not the I/O
2. separate concerns:representation — grammar — errors
3. easy on the eyes, given C++
4. easy to extend & debug
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 7 / 45
![Page 19: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/19.jpg)
Practical Boost.Spirit The Spirit within Boost
Boost.Spirit framework anno 2010
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 8 / 45
![Page 20: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/20.jpg)
Practical Boost.Spirit The legacy of legacy C++
Boosting compatibility
Boost provides compatibility with nearly any platform and toolchain.
Spirit was wri�en before C++ 11/14/17/20.
Built on top of some heavy-weight Boost components:
• MPL: meta template programming
• Phoenix: functional programming
• Fusion: generic sequence programming, limited introspection
• Preprocessor: macro magic
On the upside: the power of these libraries is well integrated into the constructs ofSpirit.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 9 / 45
![Page 21: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/21.jpg)
Practical Boost.Spirit The legacy of legacy C++
Boosting compatibility
Boost provides compatibility with nearly any platform and toolchain.
Spirit was wri�en before C++ 11/14/17/20.
Built on top of some heavy-weight Boost components:
• MPL: meta template programming
• Phoenix: functional programming
• Fusion: generic sequence programming, limited introspection
• Preprocessor: macro magic
On the upside: the power of these libraries is well integrated into the constructs ofSpirit.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 9 / 45
![Page 22: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/22.jpg)
Practical Boost.Spirit The legacy of legacy C++
Boosting compatibility
Boost provides compatibility with nearly any platform and toolchain.
Spirit was wri�en before C++ 11/14/17/20.
Built on top of some heavy-weight Boost components:
• MPL: meta template programming
• Phoenix: functional programming
• Fusion: generic sequence programming, limited introspection
• Preprocessor: macro magic
On the upside: the power of these libraries is well integrated into the constructs ofSpirit.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 9 / 45
![Page 23: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/23.jpg)
Practical Boost.Spirit Unleashing C++14
Boost.Spirit.X3
Focuses on the parser component – replaces Qi
Dropped Phoenix integration which can be replaced quite lightly by e.g. C++ 14lambdas and other functional features of modern C++ .
Seems to be somewhat feature-complete, but lacking some notable useful bits fromits predecessors.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 10 / 45
![Page 24: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/24.jpg)
Practical Boost.Spirit Elementary elements
Basic concepts
• parser: object describing the form of a specific form of input:
x3 : : doub le_
• a�ribute: synthesized “result” of a parser, can be nothing (unused_type)
• skipper: object describing what consists of “whitespace” and is skipped overwhen looking for the next matching token.
• rule: an agglomeration of parsers providing a�ribute scope and metadata forerror handling and debugging.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 11 / 45
![Page 25: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/25.jpg)
Practical Boost.Spirit Elementary elements
Basic concepts
• parser: object describing the form of a specific form of input:
x3 : : doub le_
• a�ribute: synthesized “result” of a parser, can be nothing (unused_type)
• skipper: object describing what consists of “whitespace” and is skipped overwhen looking for the next matching token.
• rule: an agglomeration of parsers providing a�ribute scope and metadata forerror handling and debugging.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 11 / 45
![Page 26: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/26.jpg)
Practical Boost.Spirit Elementary elements
Basic concepts
• parser: object describing the form of a specific form of input:
x3 : : doub le_
• a�ribute: synthesized “result” of a parser, can be nothing (unused_type)
• skipper: object describing what consists of “whitespace” and is skipped overwhen looking for the next matching token.
• rule: an agglomeration of parsers providing a�ribute scope and metadata forerror handling and debugging.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 11 / 45
![Page 27: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/27.jpg)
Practical Boost.Spirit Elementary elements
Basic concepts
• parser: object describing the form of a specific form of input:
x3 : : doub le_
• a�ribute: synthesized “result” of a parser, can be nothing (unused_type)
• skipper: object describing what consists of “whitespace” and is skipped overwhen looking for the next matching token.
• rule: an agglomeration of parsers providing a�ribute scope and metadata forerror handling and debugging.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 11 / 45
![Page 28: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/28.jpg)
Practical Boost.Spirit Please, enter
Parsing an input range
Two main entry points:
b o o l s u c c e s s = p a r s e ( f i r s t , l a s t ,p a r s e r ,r e s u l t ) ;
b o o l s u c c e s s = p h r a s e _ p a r s e ( f i r s t , l a s t ,p a r s e r ,s k i p p e r ,r e s u l t ) ;
Parser matched if success == true.
Complete match if also first == last.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 12 / 45
![Page 29: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/29.jpg)
Practical Boost.Spirit Enter the parser
Test bed boilerplate
i n t main ( ){
s t d : : s t r i n g i n p u t = / ∗ b y t e s we want p a r s e d ∗ / ;const auto p a r s e r = / ∗ T h i s i s where t h e magic g o e s ∗ / ;
auto f i r s t = i n p u t . beg in ( ) ;auto l a s t = i n p u t . end ( ) ;s t d : : s t r i n g r e s u l t ;bool s u c c e s s = x3 : : p a r s e ( f i r s t , l a s t ,
p a r s e r ,r e s u l t ) ;
i f ( ! s u c c e s s )s t d : : c e r r << " p a r s i n g f a i l e d . \ n " ;
e l s e i f ( f i r s t ! = l a s t )s t d : : c e r r << " p a r s i n g d id not match f u l l i n p u t . \ n " ;
e l s es t d : : cout << " r e s u l t : " << r e s u l t << ' \ n ' ;
}
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 13 / 45
![Page 30: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/30.jpg)
Practical Boost.Spirit Enter the parser
Li�le bit of explanation
Example parser:
p a r s e r e x p r e s s i o n → a t t r i b u t e type
In case of no a�ribute: x3::unused_type
Example input:
3 . 1 4 → 3 . 1 4
Spirit is greedy, so partial matches are o�en possible!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 14 / 45
![Page 31: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/31.jpg)
Practical Boost.Spirit Enter the parser
Li�le bit of explanation
Example parser:
p a r s e r e x p r e s s i o n → a t t r i b u t e type
In case of no a�ribute: x3::unused_type
Example input:
3 . 1 4 → 3 . 1 4
Spirit is greedy, so partial matches are o�en possible!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 14 / 45
![Page 32: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/32.jpg)
Practical Boost.Spirit Enter the parser
Warming up
doub le_ → doub le
Matches:
3 . 1 4 → 3 . 1 41e−10 → . 0 0 0 0 0 0 0 0 0 1−5 → −5.
Does not match:
a s t r i n g0 xFF → 0 .mY_S1lly_P@sSw0rD
i n t _ → i n t
Matches:
0 → 02 → 2−3 → −3
Does not match:
3 . 1 4 1 5 → 3EDA34CF−1e0 → −1
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 15 / 45
![Page 33: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/33.jpg)
Practical Boost.Spirit Enter the parser
Warming up
doub le_ → doub le
Matches:
3 . 1 4 → 3 . 1 41e−10 → . 0 0 0 0 0 0 0 0 0 1−5 → −5.
Does not match:
a s t r i n g0 xFF → 0 .mY_S1lly_P@sSw0rD
i n t _ → i n t
Matches:
0 → 02 → 2−3 → −3
Does not match:
3 . 1 4 1 5 → 3EDA34CF−1e0 → −1
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 15 / 45
![Page 34: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/34.jpg)
Practical Boost.Spirit Enter the parser
Warming up
doub le_ → doub le
Matches:
3 . 1 4 → 3 . 1 41e−10 → . 0 0 0 0 0 0 0 0 0 1−5 → −5.
Does not match:
a s t r i n g0 xFF → 0 .mY_S1lly_P@sSw0rD
i n t _ → i n t
Matches:
0 → 02 → 2−3 → −3
Does not match:
3 . 1 4 1 5 → 3EDA34CF−1e0 → −1
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 15 / 45
![Page 35: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/35.jpg)
Practical Boost.Spirit Enter the parser
Warming up
doub le_ → doub le
Matches:
3 . 1 4 → 3 . 1 41e−10 → . 0 0 0 0 0 0 0 0 0 1−5 → −5.
Does not match:
a s t r i n g0 xFF → 0 .mY_S1lly_P@sSw0rD
i n t _ → i n t
Matches:
0 → 02 → 2−3 → −3
Does not match:
3 . 1 4 1 5 → 3EDA34CF−1e0 → −1
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 15 / 45
![Page 36: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/36.jpg)
Practical Boost.Spirit Enter the parser
Warming up
doub le_ → doub le
Matches:
3 . 1 4 → 3 . 1 41e−10 → . 0 0 0 0 0 0 0 0 0 1−5 → −5.
Does not match:
a s t r i n g0 xFF → 0 .mY_S1lly_P@sSw0rD
i n t _ → i n t
Matches:
0 → 02 → 2−3 → −3
Does not match:
3 . 1 4 1 5 → 3EDA34CF−1e0 → −1
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 15 / 45
![Page 37: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/37.jpg)
Practical Boost.Spirit Enter the parser
Warming up
doub le_ → doub le
Matches:
3 . 1 4 → 3 . 1 41e−10 → . 0 0 0 0 0 0 0 0 0 1−5 → −5.
Does not match:
a s t r i n g0 xFF → 0 .mY_S1lly_P@sSw0rD
i n t _ → i n t
Matches:
0 → 02 → 2−3 → −3
Does not match:
3 . 1 4 1 5 → 3EDA34CF−1e0 → −1
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 15 / 45
![Page 38: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/38.jpg)
Practical Boost.Spirit Enter the parser
Numeric parsers
Number parsers:
f l o a t _ doub le_ l o n g _ d o u b l eb in o c t hexu s h o r t _ u long_ u i n t _ u l o n g _ l o n gs h o r t _ long_ i n t _ l o n g _ l o n g
Literal forms for all of the above except bin, oct, and hex:
f l o a t _ ( 3 . 1 4 f ) → unused_typeu s h o r t _ ( 4 2 ) → unused_typei n t _ ( 3 6 5 ) → unused_type
Guts to define custom numeric parsers
[ u ] i n t _ p a r s e r <T , Radix , MinDig i t s , MaxDigits >r e a l _ p a r s e r <T , R e a l P o l i c i e s >
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 16 / 45
![Page 39: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/39.jpg)
Practical Boost.Spirit Enter the parser
Numeric parsers
Number parsers:
f l o a t _ doub le_ l o n g _ d o u b l eb in o c t hexu s h o r t _ u long_ u i n t _ u l o n g _ l o n gs h o r t _ long_ i n t _ l o n g _ l o n g
Literal forms for all of the above except bin, oct, and hex:
f l o a t _ ( 3 . 1 4 f ) → unused_typeu s h o r t _ ( 4 2 ) → unused_typei n t _ ( 3 6 5 ) → unused_type
Guts to define custom numeric parsers
[ u ] i n t _ p a r s e r <T , Radix , MinDig i t s , MaxDigits >r e a l _ p a r s e r <T , R e a l P o l i c i e s >
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 16 / 45
![Page 40: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/40.jpg)
Practical Boost.Spirit Enter the parser
Numeric parsers
Number parsers:
f l o a t _ doub le_ l o n g _ d o u b l eb in o c t hexu s h o r t _ u long_ u i n t _ u l o n g _ l o n gs h o r t _ long_ i n t _ l o n g _ l o n g
Literal forms for all of the above except bin, oct, and hex:
f l o a t _ ( 3 . 1 4 f ) → unused_typeu s h o r t _ ( 4 2 ) → unused_typei n t _ ( 3 6 5 ) → unused_type
Guts to define custom numeric parsers
[ u ] i n t _ p a r s e r <T , Radix , MinDig i t s , MaxDigits >r e a l _ p a r s e r <T , R e a l P o l i c i e s >
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 16 / 45
![Page 41: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/41.jpg)
Practical Boost.Spirit Enter the parser
Warming up...
" a s t r i n g " → unused_type
Matches
a s t r i n g → unused_type
Does not match:
not a s t r i n g
alnum → char
Matches:
a → ' a 'T → 'T 'R → 'R 'Q → 'Q '
Does not match:
f o u r → ' f '_∗&
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 17 / 45
![Page 42: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/42.jpg)
Practical Boost.Spirit Enter the parser
Warming up...
" a s t r i n g " → unused_type
Matches
a s t r i n g → unused_type
Does not match:
not a s t r i n g
alnum → char
Matches:
a → ' a 'T → 'T 'R → 'R 'Q → 'Q '
Does not match:
f o u r → ' f '_∗&
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 17 / 45
![Page 43: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/43.jpg)
Practical Boost.Spirit Enter the parser
Warming up...
" a s t r i n g " → unused_type
Matches
a s t r i n g → unused_type
Does not match:
not a s t r i n g
alnum → char
Matches:
a → ' a 'T → 'T 'R → 'R 'Q → 'Q '
Does not match:
f o u r → ' f '_∗&
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 17 / 45
![Page 44: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/44.jpg)
Practical Boost.Spirit Enter the parser
Warming up...
" a s t r i n g " → unused_type
Matches
a s t r i n g → unused_type
Does not match:
not a s t r i n g
alnum → char
Matches:
a → ' a 'T → 'T 'R → 'R 'Q → 'Q '
Does not match:
f o u r → ' f '_∗&
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 17 / 45
![Page 45: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/45.jpg)
Practical Boost.Spirit Enter the parser
Warming up...
" a s t r i n g " → unused_type
Matches
a s t r i n g → unused_type
Does not match:
not a s t r i n g
alnum → char
Matches:
a → ' a 'T → 'T 'R → 'R 'Q → 'Q '
Does not match:
f o u r → ' f '_∗&
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 17 / 45
![Page 46: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/46.jpg)
Practical Boost.Spirit Enter the parser
Warming up...
" a s t r i n g " → unused_type
Matches
a s t r i n g → unused_type
Does not match:
not a s t r i n g
alnum → char
Matches:
a → ' a 'T → 'T 'R → 'R 'Q → 'Q '
Does not match:
f o u r → ' f '_∗&
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 17 / 45
![Page 47: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/47.jpg)
Practical Boost.Spirit Enter the parser
Character parsers
Literals:
' a ' → unused_typel i t ( ' a ' ) → unused_typechar_ ( ' a ' ) → unused_typechar_ ( " abc " ) → unused_typechar_ ( ' a ' , ' z ' ) → unused_typechar_ ( c h a r s e t ) → unused_type
These correspond to the std::is... functions:
alnum alpha b lankc n t r l d i g i t graphp r i n t punct spacex d i g i t l ower upper
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 18 / 45
![Page 48: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/48.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 49: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/49.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
f l o a t _ >> i n t _ → t u p l e < f l o a t , i n t >
Matches:
3 . 1 4 5 → { 3 . 1 4 f , 5 }5 42 → { 5 . f , 4 2 }
Does not match:
5 3 . 1 4 → { 5 . f , 3 }3 . 1 abc
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 50: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/50.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
f l o a t _ | i n t → v a r i a n t < f l o a t , i n t >
Matches:
3 . 1 4 → 3 . 1 4 f5 → 5
Does not match:
abc3 . 1 4 5 → 3 . 1 4 f
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 51: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/51.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
! f l o a t _ >> i n t _ → i n t
Matches:
a 5 → 5∗ 42 → 42
Does not match:
3 . 1 4 5∗ 3 . 1 4 → 3
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 52: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/52.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
&f l o a t _ >> i n t _ → i n t
Matches:
3 . 1 4 5 → 51e−10 20 → 20
Does not match:
53 . 1 4 abc
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 53: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/53.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
f l o a t _ >> − i n t _ → t u p l e < f l o a t , o p t i o n a l < i n t >>
Matches:
3 . 1 4 5 → { 3 . 1 4 f , 5 }3 . 1 4 → { 3 . 1 4 f , { } }
Does not match:
5 3 . 1 4 → { 5 . f , 3 }3 . 1 4 a → { 3 . 1 4 f , { } }
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 54: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/54.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
∗ f l o a t _ → v e c t o r < f l o a t >
Matches:
→ { }3 . 1 4 5 → { 3 . 1 4 f , 5 . f }
Does not match:
3 . 1 4 a 5 → { 3 . 1 4 }a 5 3 . 1 4
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 55: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/55.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
+ i n t _ → v e c t o r < i n t >
Matches:
1 2 3 → { 1 , 2 , 3 }9 → { 9 }
Does not match:
5 3 . 1 4 → { 5 , 3 }
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 56: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/56.jpg)
Practical Boost.Spirit Throwing it all together
Parser operators
Expression A�ribute Description!a unused fails if a matches&a unused matches if a matches-a optional<A> match a zero or one time*a vector<A> match a zero or more times+a vector<A> match a one or more times
a | b variant<A, B> alternative: a or ba % b vector<A> list of a delimited by ba >> b tuple<A, B> sequence: a followed by b
i n t _ % ' , ' → v e c t o r < i n t >
Matches:
−3 , 3 → {−3 , 3 }1 → { 1 }
Does not match:
1 2 3 → { 1 }
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 19 / 45
![Page 57: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/57.jpg)
Practical Boost.Spirit Strings are the threads of reality
String parsers
" l i t e r a l s t r i n g "l i t ( " l i t e r a l s t r i n g " )s t r i n g ( " l i t e r a l s t r i n g " )
Due to C++ grammar, the lit helper is only needed if a parser expression beginswith a literal.
" number " >> ' : ' >> doub le
Needs to be
l i t ( " number " ) >> ' : ' >> doub le_
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 20 / 45
![Page 58: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/58.jpg)
Practical Boost.Spirit Free elegance
Symbol table
Symbol table:
enum d i g i t { one , two , / ∗ . . . ∗ / n ine } ;s t r u c t t a b l e : x3 : : symbols < s t d : : u i n t 8 _ t >{
t a b l e ( ) { add ( " one " , one ) ( " two " , two ) / ∗ . . ∗ / ( " n ine " , n ine ) ; }} c o n s t d i g i t s ;
Use as
d i g i t s → d i g i t
Matches:
one → 1two → 2n ine → 9
Does not match:
f o r t y−twoone hundred → 1m i l l i o n s
Powerful to parse directly to e.g. enums, but also more complicated types!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 21 / 45
![Page 59: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/59.jpg)
Practical Boost.Spirit Free elegance
Symbol table
Symbol table:
enum d i g i t { one , two , / ∗ . . . ∗ / n ine } ;s t r u c t t a b l e : x3 : : symbols < s t d : : u i n t 8 _ t >{
t a b l e ( ) { add ( " one " , one ) ( " two " , two ) / ∗ . . ∗ / ( " n ine " , n ine ) ; }} c o n s t d i g i t s ;
Use as
d i g i t s → d i g i t
Matches:
one → 1two → 2n ine → 9
Does not match:
f o r t y−twoone hundred → 1m i l l i o n s
Powerful to parse directly to e.g. enums, but also more complicated types!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 21 / 45
![Page 60: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/60.jpg)
Practical Boost.Spirit Free elegance
Symbol table
Symbol table:
enum d i g i t { one , two , / ∗ . . . ∗ / n ine } ;s t r u c t t a b l e : x3 : : symbols < s t d : : u i n t 8 _ t >{
t a b l e ( ) { add ( " one " , one ) ( " two " , two ) / ∗ . . ∗ / ( " n ine " , n ine ) ; }} c o n s t d i g i t s ;
Use as
d i g i t s → d i g i t
Matches:
one → 1two → 2n ine → 9
Does not match:
f o r t y−twoone hundred → 1m i l l i o n s
Powerful to parse directly to e.g. enums, but also more complicated types!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 21 / 45
![Page 61: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/61.jpg)
Practical Boost.Spirit Free elegance
Symbol table
Symbol table:
enum d i g i t { one , two , / ∗ . . . ∗ / n ine } ;s t r u c t t a b l e : x3 : : symbols < s t d : : u i n t 8 _ t >{
t a b l e ( ) { add ( " one " , one ) ( " two " , two ) / ∗ . . ∗ / ( " n ine " , n ine ) ; }} c o n s t d i g i t s ;
Use as
d i g i t s → d i g i t
Matches:
one → 1two → 2n ine → 9
Does not match:
f o r t y−twoone hundred → 1m i l l i o n s
Powerful to parse directly to e.g. enums, but also more complicated types!Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 21 / 45
![Page 62: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/62.jpg)
Practical Boost.Spirit You can’t live with ’em...
Auxiliary parsers
Special parsers useful in pre- and post-processing the a�ribute:Expression A�ribute Description
eol unused matches end of lineoei unused matches end of inputeps unused matches empty string (i.e. always)
eps(b) unused matches empty string if b == trueattr(v) decltype(v) consumes no input,
produces v as a�ribute
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 22 / 45
![Page 63: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/63.jpg)
Practical Boost.Spirit Guiding parsers in the right direction
Parser directives
Expression A�ribute Descriptionlexeme[a] A disable skipper, pre-skipno_skip[a] A disable skipperno_case[a] A inhibit case-sensitivityomit[a] unused ignore a�ribute of a
matches[a] bool return true if a matchesraw[a] iterator_range returns iterator range
expect[a] A throws exception if a does not matchrepeat[a] vector<A> repeat a zero or more timesskip[a] A force skipping in lexeme or no_skip
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 23 / 45
![Page 64: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/64.jpg)
Practical Boost.Spirit To act or not to act
Semantic actions
When you want to act on (the a�ribute of) a parser:
p a r s e r [ a c t i o n ]
This is useful for e.g.
• logging
• verification of external conditions
• other functionality not covered by the expressed a�ribute grammar
Required prototypes:
template < typename ContextType >v o i d a c t i o n ( c o n s t ContextType& c o n t e x t ) ;
c o n s t auto a c t i o n = [ ] ( auto& c o n t e x t ) { / ∗ . . . . ∗ / } ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 24 / 45
![Page 65: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/65.jpg)
Practical Boost.Spirit To act or not to act
Semantic actions
When you want to act on (the a�ribute of) a parser:
p a r s e r [ a c t i o n ]
This is useful for e.g.
• logging
• verification of external conditions
• other functionality not covered by the expressed a�ribute grammar
Required prototypes:
template < typename ContextType >v o i d a c t i o n ( c o n s t ContextType& c o n t e x t ) ;
c o n s t auto a c t i o n = [ ] ( auto& c o n t e x t ) { / ∗ . . . . ∗ / } ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 24 / 45
![Page 66: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/66.jpg)
Practical Boost.Spirit To act or not to act
Semantic actions
When you want to act on (the a�ribute of) a parser:
p a r s e r [ a c t i o n ]
This is useful for e.g.
• logging
• verification of external conditions
• other functionality not covered by the expressed a�ribute grammar
Required prototypes:
template < typename ContextType >v o i d a c t i o n ( c o n s t ContextType& c o n t e x t ) ;
c o n s t auto a c t i o n = [ ] ( auto& c o n t e x t ) { / ∗ . . . . ∗ / } ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 24 / 45
![Page 67: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/67.jpg)
Practical Boost.Spirit Context is everything
Semantic action context
Function Description_val A reference to the a�ribute of the innermost rule invoking the parser
_where Iterator range to the input stream_attr A reference to the a�ribute of the parser_pass A reference to a bool flag that can be used to force the parser to fail
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 25 / 45
![Page 68: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/68.jpg)
Practical Boost.Spirit Context is everything
Semantic action context
Function Description_val A reference to the a�ribute of the innermost rule invoking the parser
_where Iterator range to the input stream_attr A reference to the a�ribute of the parser_pass A reference to a bool flag that can be used to force the parser to fail
Example:Divide rule value by 100:
_ v a l ( c o n t e x t ) = / 1 0 0 . ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 25 / 45
![Page 69: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/69.jpg)
Practical Boost.Spirit Context is everything
Semantic action context
Function Description_val A reference to the a�ribute of the innermost rule invoking the parser
_where Iterator range to the input stream_attr A reference to the a�ribute of the parser_pass A reference to a bool flag that can be used to force the parser to fail
Example:Increment the rule’s value with the parsed a�ribute:
_ v a l ( c t x ) += _ a t t r ( c t x ) ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 25 / 45
![Page 70: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/70.jpg)
Practical Boost.Spirit Context is everything
Semantic action context
Function Description_val A reference to the a�ribute of the innermost rule invoking the parser
_where Iterator range to the input stream_attr A reference to the a�ribute of the parser_pass A reference to a bool flag that can be used to force the parser to fail
Example:Make the parser fail:
_pass ( c t x ) = f a l s e ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 25 / 45
![Page 71: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/71.jpg)
Practical Boost.Spirit Lights, camera, action!
Actionable examples
i n t main ( ){
s t d : : s t r i n g i n p u t = " 1 2 3 " ;auto f i r s t = i n p u t . beg in ( ) ;auto l a s t = i n p u t . end ( ) ;
i n t v a l u e { } ;c o n s t auto add = [& v a l u e ] ( auto& c o n t e x t ) { v a l u e += x3 : : _ a t t r ( c o n t e x t ) ; } ;b o o l r e s u l t = x3 : : p h r a s e _ p a r s e ( f i r s t , l a s t , ∗ x3 : : i n t _ [ add ] , x3 : : b lank ) ;
i f ( r e s u l t ){
i f ( f i r s t == l a s t )s t d : : cout << " p a r s e s u c c e s f u l : " << v a l u e << ' \ n ' ;
e l s es t d : : cout << " i n c o m p l e t e p a r s e : " << v a l u e << ' \ n ' ;
}e l s e
s t d : : cout << " p a r s e u n s u c c e s f u l \ n " ;}
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 26 / 45
![Page 72: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/72.jpg)
Practical Boost.Spirit The rules that hold our society together
Rule
Rules are named parsers. They help with
• debugging
• error reporting
• boundaries of semantic actions
Defining BOOST_SPIRIT_X3_DEBUG gives debug output:
< l ength >< t ry >2cm< / t ry >
< s u c c e s s > </ s u c c e s s >< a t t r i b u t e s > [ 2 , cm] < / a t t r i b u t e s >
</ l ength >
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 27 / 45
![Page 73: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/73.jpg)
Practical Boost.Spirit The rules that hold our society together
Rule
Rules are named parsers. They help with
• debugging
• error reporting
• boundaries of semantic actions
Defining BOOST_SPIRIT_X3_DEBUG gives debug output:
< l ength >< t ry >2cm< / t ry >
< s u c c e s s > </ s u c c e s s >< a t t r i b u t e s > [ 2 , cm] < / a t t r i b u t e s >
</ l ength >
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 27 / 45
![Page 74: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/74.jpg)
Practical Boost.Spirit The rules that hold our society together
Rule syntax
Defining a rule:
c o n s t auto a _ r u l e = x3 : : r u l e < s t r u c t tag ,a t t r i b u t e _ t y p e >{ " r u l e name " }
= / ∗ a c t u a l p a r s e r ∗ / ;
In the presence of semantic actions, auto a�ribute propagation is disabled.Re-enable with %=
%= / ∗ a c t u a l p a r s e r ∗ / ;
There are also ways of spli�ing declaration/definition through several macro’s,
BOOST_SPIRIT_DECLAREBOOST_SPIRIT_DEFINEBOOST_SPIRIT_INSTANTIATE
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 28 / 45
![Page 75: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/75.jpg)
Practical Boost.Spirit The rules that hold our society together
Rule syntax
Defining a rule:
c o n s t auto a _ r u l e = x3 : : r u l e < s t r u c t tag ,a t t r i b u t e _ t y p e >{ " r u l e name " }
= / ∗ a c t u a l p a r s e r ∗ / ;
In the presence of semantic actions, auto a�ribute propagation is disabled.Re-enable with %=
%= / ∗ a c t u a l p a r s e r ∗ / ;
There are also ways of spli�ing declaration/definition through several macro’s,
BOOST_SPIRIT_DECLAREBOOST_SPIRIT_DEFINEBOOST_SPIRIT_INSTANTIATE
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 28 / 45
![Page 76: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/76.jpg)
Practical Boost.Spirit The rules that hold our society together
Rule syntax
Defining a rule:
c o n s t auto a _ r u l e = x3 : : r u l e < s t r u c t tag ,a t t r i b u t e _ t y p e >{ " r u l e name " }
= / ∗ a c t u a l p a r s e r ∗ / ;
In the presence of semantic actions, auto a�ribute propagation is disabled.Re-enable with %=
%= / ∗ a c t u a l p a r s e r ∗ / ;
There are also ways of spli�ing declaration/definition through several macro’s,
BOOST_SPIRIT_DECLAREBOOST_SPIRIT_DEFINEBOOST_SPIRIT_INSTANTIATE
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 28 / 45
![Page 77: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/77.jpg)
CSS
![Page 78: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/78.jpg)
CSS Waterfalls. . .with style
Cascading Style Sheets
Hierarchical description of styling
Lives on the web
General enough to apply to other domains:
• XUL: Mozilla’s XML User Interface Language
• Pango Text A�ribute Markup Language
• QWidgets use QSS, a CSS dialect
• QML uses inline CSS-ish properties
• various derivatives using similar property names and values
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 29 / 45
![Page 79: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/79.jpg)
CSS Hello Web!
Example
body {c o l o r : # 3 2 5 0 5 0 ;background : # f f f ;font−f a m i l y : A r i a l , sans−s e r i f ;font−s i z e : 70%;
}
More formally:
d e c l a r a t i o n = proper ty , " : " , va lue , " ; " ;r u l e _ s e t = s e l e c t o r , " { " , d e c l a r a t i o n + , " } " ;
Simple enough, right?
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 30 / 45
![Page 80: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/80.jpg)
CSS Hello Web!
The Devil is in the details
Some details previously omi�ed for brevity:
• the format of value depends on which property
background−c o l o r : # f 7 f ;border−top−width : 1 px ;border−c o l l a p s e : c o l l a p s e ;
• properties such as background-color are more specific thanbackground:
background : aquamarine u r l ( " faded . png " )no−r e p e a t f i x e d c e n t e r ;
• unknown properties must be ignored
• varying levels of extensions and strictness in existing implementations
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 31 / 45
![Page 81: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/81.jpg)
CSS Hello Web!
The Devil is in the details
Some details previously omi�ed for brevity:
• the format of value depends on which property
background−c o l o r : # f 7 f ;border−top−width : 1 px ;border−c o l l a p s e : c o l l a p s e ;
• properties such as background-color are more specific thanbackground:
background : aquamarine u r l ( " faded . png " )no−r e p e a t f i x e d c e n t e r ;
• unknown properties must be ignored
• varying levels of extensions and strictness in existing implementations
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 31 / 45
![Page 82: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/82.jpg)
CSS Hello Web!
The Devil is in the details
Some details previously omi�ed for brevity:
• the format of value depends on which property
background−c o l o r : # f 7 f ;border−top−width : 1 px ;border−c o l l a p s e : c o l l a p s e ;
• properties such as background-color are more specific thanbackground:
background : aquamarine u r l ( " faded . png " )no−r e p e a t f i x e d c e n t e r ;
• unknown properties must be ignored
• varying levels of extensions and strictness in existing implementations
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 31 / 45
![Page 83: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/83.jpg)
CSS Hello Web!
The Devil is in the details
Some details previously omi�ed for brevity:
• the format of value depends on which property
background−c o l o r : # f 7 f ;border−top−width : 1 px ;border−c o l l a p s e : c o l l a p s e ;
• properties such as background-color are more specific thanbackground:
background : aquamarine u r l ( " faded . png " )no−r e p e a t f i x e d c e n t e r ;
• unknown properties must be ignored
• varying levels of extensions and strictness in existing implementations
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 31 / 45
![Page 84: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/84.jpg)
CSS in Spirit
![Page 85: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/85.jpg)
CSS in Spirit Expectations ruin dreams
Extensive grammar and required
Basic CSS syntax:
s e l e c t o r { p r o p e r t y : v a l u e ; }
Every CSS property has di�erent possible values with possibly other interpretations.
Some properties possibly take multiple values, with di�erent behaviour if less arespecified.
Some things are used in multiple (sub-)properties:
• length (relative and absolute)
• colour
• . . .
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 32 / 45
![Page 86: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/86.jpg)
CSS in Spirit Expectations ruin dreams
Extensive grammar and required
Basic CSS syntax:
s e l e c t o r { p r o p e r t y : v a l u e ; }
Every CSS property has di�erent possible values with possibly other interpretations.
Some properties possibly take multiple values, with di�erent behaviour if less arespecified.
Some things are used in multiple (sub-)properties:
• length (relative and absolute)
• colour
• . . .
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 32 / 45
![Page 87: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/87.jpg)
CSS in Spirit Expectations ruin dreams
Extensive grammar and required
Basic CSS syntax:
s e l e c t o r { p r o p e r t y : v a l u e ; }
Every CSS property has di�erent possible values with possibly other interpretations.
Some properties possibly take multiple values, with di�erent behaviour if less arespecified.
Some things are used in multiple (sub-)properties:
• length (relative and absolute)
• colour
• . . .
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 32 / 45
![Page 88: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/88.jpg)
CSS in Spirit Expectations ruin dreams
Extensive grammar and required
Basic CSS syntax:
s e l e c t o r { p r o p e r t y : v a l u e ; }
Every CSS property has di�erent possible values with possibly other interpretations.
Some properties possibly take multiple values, with di�erent behaviour if less arespecified.
Some things are used in multiple (sub-)properties:
• length (relative and absolute)
• colour
• . . .
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 32 / 45
![Page 89: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/89.jpg)
CSS in Spirit Jumping right in
Skipping straight to the properties
Let’s ignore the complexity of selectors for now and assume we have a greatselector parser:
c o n s t auto s e l e c t o r = x3 : : r u l e < s t r u c t s e l e c t o r ,c s s : : s e l e c t o r >{ " s e l e c t o r " }
= ∗ ( char_ − ' { ' ) ; / / not so g r e a t . . .
c o n s t auto d e c l a r a t i o n _ b l o c k= x3 : : r u l e < s t r u c t d e c l a r a t i o n _ b l o c k ,
c s s : : d e c l a r a t i o n _ b l o c k >{ " d e c l a r a t i o n _ b l o c k " }
= l i t ( ' { ' ) >> + d e c l a r a t i o n >> ' } ' ;
c o n s t auto r u l e _ s e t = x3 : : r u l e < s t r u c t r u l e _ s e t ,c s s : : r u l e _ s e t ,{ " r u l e−s e t " } ;
= s e l e c t o r >> d e c l a r a t i o n _ b l o c k ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 33 / 45
![Page 90: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/90.jpg)
CSS in Spirit Jumping right in
Skipping straight to the properties
Let’s ignore the complexity of selectors for now and assume we have a greatselector parser:
c o n s t auto s e l e c t o r = x3 : : r u l e < s t r u c t s e l e c t o r ,c s s : : s e l e c t o r >{ " s e l e c t o r " }
= ∗ ( char_ − ' { ' ) ; / / not so g r e a t . . .
c o n s t auto d e c l a r a t i o n _ b l o c k= x3 : : r u l e < s t r u c t d e c l a r a t i o n _ b l o c k ,
c s s : : d e c l a r a t i o n _ b l o c k >{ " d e c l a r a t i o n _ b l o c k " }
= l i t ( ' { ' ) >> + d e c l a r a t i o n >> ' } ' ;
c o n s t auto r u l e _ s e t = x3 : : r u l e < s t r u c t r u l e _ s e t ,c s s : : r u l e _ s e t ,{ " r u l e−s e t " } ;
= s e l e c t o r >> d e c l a r a t i o n _ b l o c k ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 33 / 45
![Page 91: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/91.jpg)
CSS in Spirit Jumping right in
Skipping straight to the properties
Let’s ignore the complexity of selectors for now and assume we have a greatselector parser:
c o n s t auto s e l e c t o r = x3 : : r u l e < s t r u c t s e l e c t o r ,c s s : : s e l e c t o r >{ " s e l e c t o r " }
= ∗ ( char_ − ' { ' ) ; / / not so g r e a t . . .
c o n s t auto d e c l a r a t i o n _ b l o c k= x3 : : r u l e < s t r u c t d e c l a r a t i o n _ b l o c k ,
c s s : : d e c l a r a t i o n _ b l o c k >{ " d e c l a r a t i o n _ b l o c k " }
= l i t ( ' { ' ) >> + d e c l a r a t i o n >> ' } ' ;
c o n s t auto r u l e _ s e t = x3 : : r u l e < s t r u c t r u l e _ s e t ,c s s : : r u l e _ s e t ,{ " r u l e−s e t " } ;
= s e l e c t o r >> d e c l a r a t i o n _ b l o c k ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 33 / 45
![Page 92: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/92.jpg)
CSS in Spirit Jumping right in
Properties and a�ributes
Goal: direct translation of CSS to a workable in-memory representation (no extraAST layer)
Each property can take on the values inherit and initial:
i n l i n e s t r u c t i n h e r i t _ t f i n a l { } i n h e r i t ;i n l i n e s t r u c t i n i t i a l _ t f i n a l { } i n i t i a l ;
c o n s t e x p r b o o l o p e r a t o r ==( c o n s t i n h e r i t _ t & , c o n s t i n h e r i t _ t &) { r e t u r n t r u e ; }c o n s t e x p r b o o l o p e r a t o r ==( c o n s t i n i t i a l _ t & , c o n s t i n i t i a l _ t &) { r e t u r n t r u e ; }
template < typename . . . ValueTypes >u s i n g p r o p e r t y = s t d : : v a r i a n t < ValueTypes . . . ,
i n h e r i t _ t ,i n i t i a l _ t > ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 34 / 45
![Page 93: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/93.jpg)
CSS in Spirit Jumping right in
Properties and a�ributes
Goal: direct translation of CSS to a workable in-memory representation (no extraAST layer)
Each property can take on the values inherit and initial:
i n l i n e s t r u c t i n h e r i t _ t f i n a l { } i n h e r i t ;i n l i n e s t r u c t i n i t i a l _ t f i n a l { } i n i t i a l ;
c o n s t e x p r b o o l o p e r a t o r ==( c o n s t i n h e r i t _ t & , c o n s t i n h e r i t _ t &) { r e t u r n t r u e ; }c o n s t e x p r b o o l o p e r a t o r ==( c o n s t i n i t i a l _ t & , c o n s t i n i t i a l _ t &) { r e t u r n t r u e ; }
template < typename . . . ValueTypes >u s i n g p r o p e r t y = s t d : : v a r i a n t < ValueTypes . . . ,
i n h e r i t _ t ,i n i t i a l _ t > ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 34 / 45
![Page 94: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/94.jpg)
CSS in Spirit Jumping right in
Properties and a�ributes
Goal: direct translation of CSS to a workable in-memory representation (no extraAST layer)
Each property can take on the values inherit and initial:
i n l i n e s t r u c t i n h e r i t _ t f i n a l { } i n h e r i t ;i n l i n e s t r u c t i n i t i a l _ t f i n a l { } i n i t i a l ;
c o n s t e x p r b o o l o p e r a t o r ==( c o n s t i n h e r i t _ t & , c o n s t i n h e r i t _ t &) { r e t u r n t r u e ; }c o n s t e x p r b o o l o p e r a t o r ==( c o n s t i n i t i a l _ t & , c o n s t i n i t i a l _ t &) { r e t u r n t r u e ; }
template < typename . . . ValueTypes >u s i n g p r o p e r t y = s t d : : v a r i a n t < ValueTypes . . . ,
i n h e r i t _ t ,i n i t i a l _ t > ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 34 / 45
![Page 95: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/95.jpg)
CSS in Spirit Jumping right in
Declaration block
s t r u c t d e c l a r a t i o n _ b l o c k{
p rope r ty < c s s : : a l i g n _ c o n t e n t > a l i g n _ c o n t e n t { } ;p rope r ty < c s s : : a l i g n _ i t e m s > a l i g n _ i t e m s { } ;p rope r ty < c s s : : a l i g n _ s e l f > a l i g n _ s e l f { } ;/ / . . .c s s : : background background { } ;c s s : : b o r d e r b o r d e r { } ;/ / . . .
} ;
Let’s first focus on the properties themselves!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 35 / 45
![Page 96: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/96.jpg)
CSS in Spirit Jumping right in
Declaration block
s t r u c t d e c l a r a t i o n _ b l o c k{
p rope r ty < c s s : : a l i g n _ c o n t e n t > a l i g n _ c o n t e n t { } ;p rope r ty < c s s : : a l i g n _ i t e m s > a l i g n _ i t e m s { } ;p rope r ty < c s s : : a l i g n _ s e l f > a l i g n _ s e l f { } ;/ / . . .c s s : : background background { } ;c s s : : b o r d e r b o r d e r { } ;/ / . . .
} ;
Let’s first focus on the properties themselves!
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 35 / 45
![Page 97: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/97.jpg)
CSS in Spirit Jumping right in
Enum properties
namespace c s s{
enum c l a s s a l i g n _ c o n t e n t : s t d : : u i n t 8 _ t{
s t r e t c h ,c e n t e r ,f l e x _ s t a r t ,f l e x _ e n d ,space_between ,space_around
} ;}
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 36 / 45
![Page 98: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/98.jpg)
CSS in Spirit Jumping right in
Symbol tables for enums
namespace c s s : : grammar{
s t r u c t a l i g n _ c o n t e n t _ t a b l e : x3 : : symbols < c s s : : a l i g n _ c o n t e n t >{
a l i g n _ c o n t e n t _ t a b l e ( ){
u s i n g c s s : : a l i g n _ c o n t e n t ;
add ( " s t r e t c h " , a l i g n _ c o n t e n t : : s t r e t c h )( " c e n t e r " , a l i g n _ c o n t e n t : : c e n t e r )( " f l e x−s t a r t " , a l i g n _ c o n t e n t : : f l e x _ s t a r t )( " f l e x−end " , a l i g n _ c o n t e n t : : f l e x _ e n d )( " space−between " , a l i g n _ c o n t e n t : : space_between )( " space−around " , a l i g n _ c o n t e n t : : space_around );
}} c o n s t a l i g n _ c o n t e n t ;
}
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 37 / 45
![Page 99: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/99.jpg)
CSS in Spirit Jumping right in
The full property declaration
What about the rest?
a l i g n−c o n t e n t : c e n t e r ;
We could use
" a l i g n−c o n t e n t " >> ' : ' >> grammar : : a l i g n _ c o n t e n t >> ' ; '
This means
• lots of repetition for all properties.
• has an a�ribute of type css::align_content
• how can we assign a declaration_block member?
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 38 / 45
![Page 100: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/100.jpg)
CSS in Spirit Jumping right in
The full property declaration
What about the rest?
a l i g n−c o n t e n t : c e n t e r ;
We could use
" a l i g n−c o n t e n t " >> ' : ' >> grammar : : a l i g n _ c o n t e n t >> ' ; '
This means
• lots of repetition for all properties.
• has an a�ribute of type css::align_content
• how can we assign a declaration_block member?
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 38 / 45
![Page 101: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/101.jpg)
CSS in Spirit Jumping right in
The full property declaration
What about the rest?
a l i g n−c o n t e n t : c e n t e r ;
We could use
" a l i g n−c o n t e n t " >> ' : ' >> grammar : : a l i g n _ c o n t e n t >> ' ; '
This means
• lots of repetition for all properties.
• has an a�ribute of type css::align_content
• how can we assign a declaration_block member?
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 38 / 45
![Page 102: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/102.jpg)
CSS in Spirit Jumping right in
Custom parsers to the rescue!
Assigning a member with an a�ribute:
template < typename Proper tyType ,typename ParserType ,typename PointerToMemberType >
auto make_property ( c o n s t Proper tyType& prope r ty ,c o n s t Pa r se rTy pe& p a r s e r ,PointerToMemberType member )
{c o n s t auto s e t t e r = [ member ] ( auto& c o n t e x t ){
_ v a l ( c o n t e x t ) . ∗ member = s t d : : move ( _ a t t r ( c o n t e x t ) ) ;} ;r e t u r n lexeme [ p r o p e r t y ] >> ' : ' >> p a r s e r [ s e t t e r ] >> ' ; ' ;
}
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 39 / 45
![Page 103: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/103.jpg)
CSS in Spirit Jumping right in
Property declaration
c o n s t auto d e c l a r a t i o n= x3 : : r u l e < s t r u c t _ ,
c s s : : d e c l a r a t i o n _ b l o c k > { " d e c l a r a t i o n " }%= make_property ( " a l i g n−c o n t e n t " ,
grammar : : a l i g n _ c o n t e n t ,&d e c l a r a t i o n _ b l o c k : : a l i g n _ c o n t e n t )
| make_property ( " a l i g n−s e l f " ,grammar : : a l i g n _ s e l f ,&d e c l a r a t i o n _ b l o c k : : a l i g n _ s e l f )
/ / . . .;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 40 / 45
![Page 104: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/104.jpg)
CSS in Spirit Jumping right in
CSS Color
# f f 0 0 0 0# f 0 0rgb ( 2 5 5 , 0 , 0 )rgba ( 2 5 5 , 0 , 0 , 1 )redh s l ( 0 , 100% , 50%)h s l a ( 0 , 100% , 0 . 5 , 1 )
c o l o r = r u l e < s t r u c t c o l o r _ , c s s : : c o l o r > { " c o l o r " }= named_color| lexeme [ ' # ' >> ( c o l o r _ h e x _ a l p h a
| c o l o r _ h e x| c o l o r _ s h o r t _ h e x _ a l p h a| c o l o r _ s h o r t _ h e x) ]
| c o l o r _ r g b| c o l o r _ r g b a| c o l o r _ h s l| c o l o r _ h s l a;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 41 / 45
![Page 105: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/105.jpg)
CSS in Spirit Jumping right in
CSS Color
# f f 0 0 0 0# f 0 0rgb ( 2 5 5 , 0 , 0 )rgba ( 2 5 5 , 0 , 0 , 1 )redh s l ( 0 , 100% , 50%)h s l a ( 0 , 100% , 0 . 5 , 1 )
c o l o r = r u l e < s t r u c t c o l o r _ , c s s : : c o l o r > { " c o l o r " }= named_color| lexeme [ ' # ' >> ( c o l o r _ h e x _ a l p h a
| c o l o r _ h e x| c o l o r _ s h o r t _ h e x _ a l p h a| c o l o r _ s h o r t _ h e x) ]
| c o l o r _ r g b| c o l o r _ r g b a| c o l o r _ h s l| c o l o r _ h s l a;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 41 / 45
![Page 106: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/106.jpg)
CSS in Spirit Jumping right in
CSS Color: the details
s t r u c t c o l o r{
s t d : : u i n t 8 _ t red , green , b lue , a lpha ;}
named_color: symbol table as for enums
For a hexadecimal color, we adapt the struct using Boost.Fusion:
BOOST_FUSION_ADAPT_STRUCT ( s k u i : : c s s : : c o l o r ,red , green , b lue , a lpha )
c o n s t e x p r auto u i n t 8 _ h e x = x3 : : u i n t _ p a r s e r < s t d : : u i n t 8 _ t , 1 6 , 2 , 2 > { } ;c o n s t auto h e x _ c o l o r _ a l p h a = r u l e < s t r u c t hca , c s s : : c o l o r > { " hex a lpha c o l o r " }
= u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x ;c o n s t auto h e x _ c o l o r = r u l e < s t r u c t h e x _ c o l o r , c s s : : c o l o r > { " hex c o l o r " }
= u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x >> a t t r ( 2 5 5 ) ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 42 / 45
![Page 107: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/107.jpg)
CSS in Spirit Jumping right in
CSS Color: the details
s t r u c t c o l o r{
s t d : : u i n t 8 _ t red , green , b lue , a lpha ;}
named_color: symbol table as for enums
For a hexadecimal color, we adapt the struct using Boost.Fusion:
BOOST_FUSION_ADAPT_STRUCT ( s k u i : : c s s : : c o l o r ,red , green , b lue , a lpha )
c o n s t e x p r auto u i n t 8 _ h e x = x3 : : u i n t _ p a r s e r < s t d : : u i n t 8 _ t , 1 6 , 2 , 2 > { } ;c o n s t auto h e x _ c o l o r _ a l p h a = r u l e < s t r u c t hca , c s s : : c o l o r > { " hex a lpha c o l o r " }
= u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x ;c o n s t auto h e x _ c o l o r = r u l e < s t r u c t h e x _ c o l o r , c s s : : c o l o r > { " hex c o l o r " }
= u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x >> a t t r ( 2 5 5 ) ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 42 / 45
![Page 108: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/108.jpg)
CSS in Spirit Jumping right in
CSS Color: the details
s t r u c t c o l o r{
s t d : : u i n t 8 _ t red , green , b lue , a lpha ;}
named_color: symbol table as for enums
For a hexadecimal color, we adapt the struct using Boost.Fusion:
BOOST_FUSION_ADAPT_STRUCT ( s k u i : : c s s : : c o l o r ,red , green , b lue , a lpha )
c o n s t e x p r auto u i n t 8 _ h e x = x3 : : u i n t _ p a r s e r < s t d : : u i n t 8 _ t , 1 6 , 2 , 2 > { } ;c o n s t auto h e x _ c o l o r _ a l p h a = r u l e < s t r u c t hca , c s s : : c o l o r > { " hex a lpha c o l o r " }
= u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x ;c o n s t auto h e x _ c o l o r = r u l e < s t r u c t h e x _ c o l o r , c s s : : c o l o r > { " hex c o l o r " }
= u i n t 8 _ h e x >> u i n t 8 _ h e x >> u i n t 8 _ h e x >> a t t r ( 2 5 5 ) ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 42 / 45
![Page 109: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/109.jpg)
CSS in Spirit Jumping right in
CSS color: the details
What about #f00?
c o n s t auto s i n g l e _ h e x _ d i g i t = u i n t _ p a r s e r < s t d : : u i n t 8 _ t , 1 6 , 1 , 1 > { } ;c o n s t auto shor thand_hex
= r u l e < s t r u c t shorthand_hex , s t d : : u i n t 8 _ t > { " shor thand hex d i g i t " }%= s i n g l e _ h e x _ d i g i t [ m u l t i p l y { s t d : : u i n t 8 _ t { 1 7 } } ] ;
t emplate < typename ValueType >s t r u c t m u l t i p l y{
c o n s t e x p r m u l t i p l y ( ValueType v a l u e ) : f a c t o r { v a l u e } { }
template < typename ContextType >v o i d o p e r a t o r ( ) ( ContextType& c o n t e x t ) c o n s t{
u s i n g a t t r i b u t e _ t y p e = s t d : : r e m o v e _ r e f e r e n c e _ t < d e c l t y p e ( _ a t t r ( c o n t e x t ) ) > ;_ a t t r ( c o n t e x t ) = _ a t t r ( c o n t e x t ) ∗ f a c t o r ;
}
p r i v a t e :c o n s t ValueType f a c t o r ;
} ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 43 / 45
![Page 110: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/110.jpg)
CSS in Spirit Jumping right in
CSS color: the details
What about #f00?
c o n s t auto s i n g l e _ h e x _ d i g i t = u i n t _ p a r s e r < s t d : : u i n t 8 _ t , 1 6 , 1 , 1 > { } ;c o n s t auto shor thand_hex
= r u l e < s t r u c t shorthand_hex , s t d : : u i n t 8 _ t > { " shor thand hex d i g i t " }%= s i n g l e _ h e x _ d i g i t [ m u l t i p l y { s t d : : u i n t 8 _ t { 1 7 } } ] ;
t emplate < typename ValueType >s t r u c t m u l t i p l y{
c o n s t e x p r m u l t i p l y ( ValueType v a l u e ) : f a c t o r { v a l u e } { }
template < typename ContextType >v o i d o p e r a t o r ( ) ( ContextType& c o n t e x t ) c o n s t{
u s i n g a t t r i b u t e _ t y p e = s t d : : r e m o v e _ r e f e r e n c e _ t < d e c l t y p e ( _ a t t r ( c o n t e x t ) ) > ;_ a t t r ( c o n t e x t ) = _ a t t r ( c o n t e x t ) ∗ f a c t o r ;
}
p r i v a t e :c o n s t ValueType f a c t o r ;
} ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 43 / 45
![Page 111: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/111.jpg)
CSS in Spirit Jumping right in
CSS color: the details
What about #f00?
c o n s t auto s i n g l e _ h e x _ d i g i t = u i n t _ p a r s e r < s t d : : u i n t 8 _ t , 1 6 , 1 , 1 > { } ;c o n s t auto shor thand_hex
= r u l e < s t r u c t shorthand_hex , s t d : : u i n t 8 _ t > { " shor thand hex d i g i t " }%= s i n g l e _ h e x _ d i g i t [ m u l t i p l y { s t d : : u i n t 8 _ t { 1 7 } } ] ;
t emplate < typename ValueType >s t r u c t m u l t i p l y{
c o n s t e x p r m u l t i p l y ( ValueType v a l u e ) : f a c t o r { v a l u e } { }
template < typename ContextType >v o i d o p e r a t o r ( ) ( ContextType& c o n t e x t ) c o n s t{
u s i n g a t t r i b u t e _ t y p e = s t d : : r e m o v e _ r e f e r e n c e _ t < d e c l t y p e ( _ a t t r ( c o n t e x t ) ) > ;_ a t t r ( c o n t e x t ) = _ a t t r ( c o n t e x t ) ∗ f a c t o r ;
}
p r i v a t e :c o n s t ValueType f a c t o r ;
} ;
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 43 / 45
![Page 112: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/112.jpg)
CSS in Spirit About dreams and expectations
Moving on...
I started this thinking: easy syntax, quick parser.
I dove head-first in CSS
And Boost Spirit X3
... at the same time.
It’s far from complete, but taking shape quite fast.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 44 / 45
![Page 113: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/113.jpg)
CSS in Spirit About dreams and expectations
Moving on...
I started this thinking: easy syntax, quick parser.
I dove head-first in CSS
And Boost Spirit X3
... at the same time.
It’s far from complete, but taking shape quite fast.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 44 / 45
![Page 114: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/114.jpg)
CSS in Spirit About dreams and expectations
Moving on...
I started this thinking: easy syntax, quick parser.
I dove head-first in CSS
And Boost Spirit X3
... at the same time.
It’s far from complete, but taking shape quite fast.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 44 / 45
![Page 115: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/115.jpg)
CSS in Spirit About dreams and expectations
Moving on...
I started this thinking: easy syntax, quick parser.
I dove head-first in CSS
And Boost Spirit X3
... at the same time.
It’s far from complete, but taking shape quite fast.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 44 / 45
![Page 116: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/116.jpg)
CSS in Spirit About dreams and expectations
Moving on...
I started this thinking: easy syntax, quick parser.
I dove head-first in CSS
And Boost Spirit X3
... at the same time.
It’s far from complete, but taking shape quite fast.
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 44 / 45
![Page 117: Parsing CSS with Boost.Spiritbecpp.org/blog/wp-content/uploads/2019/02/Ruben-Van... · 2019-02-10 · Parsing CSS with Boost.Spirit.X3 Ruben Van Boxem February 4th 2019](https://reader033.vdocuments.mx/reader033/viewer/2022042806/5f6ca9da2993626ad2148519/html5/thumbnails/117.jpg)
CSS in Spirit About dreams and expectations
The end
You can find all the code discussed above here:
https://github.com/skui-org/skui/tree/master/css
Any questions?
Ruben Van Boxem Parsing CSS with Boost.Spirit.X3 February 4th 2019 45 / 45