Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. void Main()
  2. {
  3. ParseAndPrintChallenge(@"Bearer realm=""FooCorp"", error=invalid_token, error_description=""The access token has expired""");
  4. }
  5.  
  6. void ParseAndPrintChallenge(string input)
  7. {
  8. var challenge = Grammar.Challenge.Parse(input);
  9. Console.WriteLine($"Scheme: {challenge.Scheme}");
  10. Console.WriteLine($"Parameters:");
  11. foreach (var p in challenge.Parameters)
  12. {
  13. Console.WriteLine($"- {p.Name} = {p.Value}");
  14. }
  15. }
  16.  
  17. static class Grammar
  18. {
  19. private static readonly Parser<char> DoubleQuote = Parse.Char('"');
  20. private static readonly Parser<char> EqualSign = Parse.Char('=');
  21. private static readonly Parser<char> Backslash = Parse.Char('\\');
  22. private static readonly Parser<char> Comma = Parse.Char(',');
  23.  
  24. private static readonly Parser<char> SeparatorChar =
  25. Parse.Chars("()<>@,;:\\\"/[]?={} \t");
  26.  
  27. private static readonly Parser<char> ControlChar =
  28. Parse.Char(Char.IsControl, "Control character");
  29.  
  30. private static readonly Parser<char> TokenChar =
  31. Parse.AnyChar
  32. .Except(SeparatorChar)
  33. .Except(ControlChar);
  34.  
  35. private static readonly Parser<string> Token =
  36. TokenChar.AtLeastOnce().Text();
  37.  
  38. private static readonly Parser<char> QdText =
  39. Parse.AnyChar.Except(DoubleQuote);
  40.  
  41. private static readonly Parser<char> QuotedPair =
  42. from _ in Backslash
  43. from c in Parse.AnyChar
  44. select c;
  45.  
  46. private static readonly Parser<string> QuotedString =
  47. from open in DoubleQuote
  48. from text in QuotedPair.Or(QdText).Many().Text()
  49. from close in DoubleQuote
  50. select text;
  51.  
  52. private static readonly Parser<string> ParameterValue =
  53. Token.Or(QuotedString);
  54.  
  55. private static readonly Parser<Parameter> Parameter =
  56. from name in Token
  57. from _ in EqualSign
  58. from value in ParameterValue
  59. select new Parameter(name, value);
  60.  
  61. private static readonly Parser<char> ListDelimiter =
  62. from leading in Parse.WhiteSpace.Many()
  63. from c in Comma
  64. from trailing in Parse.WhiteSpace.Or(Comma).Many()
  65. select c;
  66.  
  67. private static readonly Parser<Parameter[]> Parameters =
  68. from p in Parameter.DelimitedBy(ListDelimiter)
  69. select p.ToArray();
  70.  
  71. public static readonly Parser<Challenge> Challenge =
  72. from scheme in Token
  73. from _ in Parse.WhiteSpace.AtLeastOnce()
  74. from parameters in Parameters
  75. select new Challenge(scheme, parameters);
  76. }
  77.  
  78. class Parameter
  79. {
  80. public Parameter(string name, string value)
  81. {
  82. Name = name;
  83. Value = value;
  84. }
  85.  
  86. public string Name { get; }
  87. public string Value { get; }
  88. }
  89.  
  90. class Challenge
  91. {
  92. public Challenge(string scheme, Parameter[] parameters)
  93. {
  94. Scheme = scheme;
  95. Parameters = parameters;
  96. }
  97. public string Scheme { get; }
  98. public Parameter[] Parameters { get; }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement