XP antivirus 2011

XP Anti-Virus 2011 or also known as Vista Anti-virus 2011 and Win 7 Anti-virus 2011 is a rogue program that will be installed on multiple operating system.

XP Antivirus

What's new in Google's Android 2.3 Gingerbread?

Part of the fun of owning an Android phone is receiving the updates -- you never know what new treats will arrive when one appears on your phone, like Santa coming down the chimney on Christmas Eve

GingerBread

Lenovo ThinkPad X1

Slimmer than Kate Moss after a month on the Slender diet is Lenovo’s gorgeous ThinkPad X1 laptop, details of which have just shimmied on to the InterWebs

Lenovo Thinkpad

Evolution of Cell Phone

Cell phones have evolved immensely since 1983, both in design and function

Evolution of Cell Phone

Samsung Galaxy S2 Review

The Samsung Galaxy S2 brings the Power of Love Samsung's history in the smartphone game has been pretty quiet – a few budget offerings, some false starts with Windows Mobile and the popular Galaxy S is pretty much it

Samsung Galaxy S2 Review

Friday, March 11, 2011

Razor Syntax: Very Sharp C# Code

Writing the parser for Razor has essentially been my job for the last 8 months, so I’d like to describe a little about some of the design ideas that went in to it as well as some of the interesting ways we implemented things.  This is the first in a few blog posts I’ll do about the Razor syntax as well as the Parser design.
Razor syntax is designed around one primary goal: Make code and markup flow together with as little interference from control characters as possible. 

  1. <ul>  
  2.     <% foreach(var p in Model.Products) { %>  
  3.     <li><%= p.Name %> ($<%= p.Price %>)</li>  
  4.     <% } %>  
  5. </ul>  
Now, let's boil it down to the parts that we actually care about, removing all of the extra ASPX control characters:
  1. <ul>  
  2.     foreach(var p in Model.Products) {  
  3.     <li>p.Name ($p.Price)</li>  
  4.     }  
  5. </ul>  
Obviously there isn't enough data here to unambiguously determine what's code and what's markup. When we were designing Razor, we started from here and added as little as we could to make it absolutely clear what is code and what is markup. We wanted Razor pages to be Code+Markup, with a little extra stuff as possible. We even used that goal as inspiration for the file extension for C# and VB Razor pages: cshtml and vbhtml.
So, using the C# Razor syntax, the above example becomes:
  1. <ul>  
  2.     @foreach(var p in Model.Products) {  
  3.     <li>@p.Name ($@p.Price)</li>  
  4.     }  
  5. </ul>  
If you ask me, that's pretty darn close to the previous example. Razor takes advantage of a deep knowledge of C# (or VB) and HTML syntaxes to infer a lot about what you intended to write. Let’s take this sample and break it down chunk by chunk to see how Razor parses this document.
  1. <ul>  
When Razor starts parsing a document anything goes until we see an "@". So this line just gets classified as Markup and we move on to the next
  1. @foreach(var p in Model.Products) {  
Here's where things get interesting. Now, Razor has found an "@". The "@" character is the magic character in Razor. One character, not 5 “<%=%>”, and we let the parser figure out the rest. If you skip ahead a bit, you’ll notice that there's nothing that indicates the end of the block of code (like the "%>" sequence in ASPX). Rather than having it's own syntax for delimiting code blocks, Razor tries to add as little as possible and just uses the syntax of the underlying language to determine when the code block is finished. In this case, Razor knows that a C# foreach statement is contained within "{" and "}" characters, so when you reach the end of the foreach block, it will go back to markup
  1. <li>@p.Name ($@p.Price)</li>  
Now, things get even more interesting. Didn't I just say we were in Code until the ending of the foreach? This looks a lot like Markup, and we’re still inside the foreach! This is another case where Razor is using the syntax of the underlying language to infer your intent. We know that after the "{" C# is expecting some kind of statement. But, instead of a statement, we see an HTML tag "<li>", so Razor infers that you intended to switch back to Markup. So we've essentially got a stack of 3 contexts: When we started out we were in Markup, then we saw @foreach so we went to Code, now we've see <li> so were back in Markup. At the closing </li> tag, we know you've finished the inner Markup block, so we go back to the body of the foreach.
  1. }  
Then we see the end of the foreach block, so we go back to the top-level Markup context.
  1. </ul>  
And we continue parsing markup until the next "@", or the end of the file. You may have noticed I skipped over a bit in the middle of the <li> tag. I'll save the details of that for my next post, but the essential logic is the same: "@" starts code, and we use C# syntax to tell us when that code block is finished

No comments: