A library such as this must be able to parse an arbitrary language
fragment, and then compute the complexity metrics over the parsed fragment.
Most metrics tools have at best a parser for the entire language,
not just a fragment, so you are likely to be hard pressed to find
many solutions.
There is one system that can provide you what you need:
our DMS Software Reengineering Toolkit. It provides parsers
for many languages (such as Java and C#;
it is unclear what language you want to analyze).
DMS has been used already to implement these kinds of metrics
for several langauges (Java, C#, JavaScript, COBOL)
and the process of doing this is straigtforward.
And DMS does parse langauge fragments, and amazingly,
the metrics implementation actually operate on such fragments.
You could customize DMS to implement exactly what you want.
See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html
and for derived metrics tools,
http://www.semdesigns.com/Products/Metrics/index.html